HandleException() public static method

Displays the details of an exception.
public static HandleException ( string caption, MethodBase method, Exception e ) : void
caption string
method System.Reflection.MethodBase
e System.Exception
return void
        /// <summary>
        /// Browses for new stores to manage.
        /// </summary>
        private void BrowseStoreBTN_Click(object sender, EventArgs e)
        {
            try
            {
                string storeType = StoreTypeCB.SelectedItem as string;
                string storePath = null;

                if (storeType == CertificateStoreType.Directory)
                {
                    FolderBrowserDialog dialog = new FolderBrowserDialog();

                    dialog.Description         = "Select Certificate Store Directory";
                    dialog.RootFolder          = Environment.SpecialFolder.MyComputer;
                    dialog.ShowNewFolderButton = true;

                    if (dialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }

                    storePath = dialog.SelectedPath;
                }

                if (storeType == CertificateStoreType.X509Store)
                {
                    CertificateStoreIdentifier store = new CertificateStoreTreeDlg().ShowDialog(null);

                    if (store == null)
                    {
                        return;
                    }

                    storePath = store.StorePath;
                }

                if (String.IsNullOrEmpty(storePath))
                {
                    return;
                }

                bool found = false;

                for (int ii = 0; ii < StorePathCB.Items.Count; ii++)
                {
                    if (String.Equals(storePath, StorePathCB.Items[ii] as string, StringComparison.OrdinalIgnoreCase))
                    {
                        StorePathCB.SelectedIndex = ii;
                        found = true;
                        break;
                    }
                }

                if (!found)
                {
                    StorePathCB.SelectedIndex = StorePathCB.Items.Add(storePath);
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#2
0
        private async void DeleteMI_Click(object sender, EventArgs e)
        {
            try
            {
                if (ItemsLV.SelectedItems.Count < 1)
                {
                    return;
                }

                DialogResult result = MessageBox.Show(
                    "Are you sure you wish to delete the certificates from the store?",
                    "Delete Certificate",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Exclamation);

                if (result != DialogResult.Yes)
                {
                    return;
                }

                // remove the certificates.
                List <ListViewItem> itemsToDelete = new List <ListViewItem>();
                bool yesToAll = false;

                using (ICertificateStore store = m_storeId.OpenStore())
                {
                    for (int ii = 0; ii < ItemsLV.SelectedItems.Count; ii++)
                    {
                        X509Certificate2 certificate = ItemsLV.SelectedItems[ii].Tag as X509Certificate2;

                        // check for private key.
                        X509Certificate2Collection certificate2 = await store.FindByThumbprint(certificate.Thumbprint);

                        if (!yesToAll && (certificate2.Count > 0) && certificate2[0].HasPrivateKey)
                        {
                            StringBuilder buffer = new StringBuilder();
                            buffer.Append("Certificate '");
                            buffer.Append(certificate2[0].Subject);
                            buffer.Append("'");
                            buffer.Append("Deleting it may cause applications to stop working.");
                            buffer.Append("\r\n");
                            buffer.Append("\r\n");
                            buffer.Append("Are you sure you wish to continue?.");

                            DialogResult yesno = new YesNoDlg().ShowDialog(buffer.ToString(), "Delete Private Key", true);

                            if (yesno == DialogResult.No)
                            {
                                continue;
                            }

                            yesToAll = yesno == DialogResult.Retry;
                        }

                        if (certificate != null)
                        {
                            await store.Delete(certificate.Thumbprint);

                            itemsToDelete.Add(ItemsLV.SelectedItems[ii]);
                        }
                    }
                }

                // remove the items.
                foreach (ListViewItem itemToDelete in itemsToDelete)
                {
                    itemToDelete.Remove();
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
                await Initialize(m_storeId, m_thumbprints);
            }
        }
        private async void ExportBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Export Certificate";

                if (m_currentDirectory == null)
                {
                    m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%LocalApplicationData%", false, false, false);
                }

                if (m_currentDirectory == null)
                {
                    m_currentDirectory = Environment.CurrentDirectory;
                }

                X509Certificate2 certificate = await m_certificate.Find();

                if (certificate == null)
                {
                    MessageBox.Show("Cannot export an invalid certificate.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string displayName = null;

                foreach (string element in Utils.ParseDistinguishedName(certificate.Subject))
                {
                    if (element.StartsWith("CN="))
                    {
                        displayName = element.Substring(3);
                        break;
                    }
                }

                StringBuilder filePath = new StringBuilder();

                if (!String.IsNullOrEmpty(displayName))
                {
                    filePath.Append(displayName);
                    filePath.Append(" ");
                }

                filePath.Append("[");
                filePath.Append(certificate.Thumbprint);
                filePath.Append("].der");

                SaveFileDialog dialog = new SaveFileDialog();

                dialog.CheckFileExists  = false;
                dialog.CheckPathExists  = true;
                dialog.DefaultExt       = ".der";
                dialog.Filter           = "Certificate Files (*.der)|*.der|All Files (*.*)|*.*";
                dialog.ValidateNames    = true;
                dialog.Title            = "Save Certificate File";
                dialog.FileName         = filePath.ToString();
                dialog.InitialDirectory = m_currentDirectory;

                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                FileInfo fileInfo = new FileInfo(dialog.FileName);
                m_currentDirectory = fileInfo.DirectoryName;

                // save the file.
                using (Stream ostrm = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None))
                {
                    byte[] data = certificate.RawData;
                    ostrm.Write(data, 0, data.Length);
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, System.Reflection.MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#4
0
        private void File_ImportMI_Click(object sender, EventArgs e) {
            try {
                if (m_exportFile == null) {
                    m_exportFile = "ComServers.endpoints.xml";
                }

                FileInfo fileInfo = new FileInfo(Utils.GetAbsoluteFilePath(m_exportFile));

                OpenFileDialog dialog = new OpenFileDialog();

                dialog.CheckFileExists = true;
                dialog.CheckPathExists = true;
                dialog.DefaultExt = ".xml";
                dialog.Filter = "Configuration Files (*.xml)|*.xml|All Files (*.*)|*.*";
                dialog.Multiselect = false;
                dialog.ValidateNames = true;
                dialog.Title = "Open Endpoint Configuration File";
                dialog.FileName = fileInfo.Name;
                dialog.InitialDirectory = fileInfo.DirectoryName;

                if (dialog.ShowDialog() != DialogResult.OK) {
                    return;
                }

                m_exportFile = dialog.FileName;

                // load the endpoints from the file.
                ConfiguredEndpointCollection endpoints = ConfiguredEndpointCollection.Load(m_exportFile);

                // update the endpoint configuration.
                StringBuilder buffer = new StringBuilder();

                foreach (ConfiguredEndpoint endpoint in endpoints.Endpoints) {
                    if (endpoint.ComIdentity == null) {
                        continue;
                    }

                    try {
                        PseudoComServer.Save(endpoint);
                    } catch (Exception exception) {
                        if (buffer.Length > 0) {
                            buffer.Append("\r\n");
                        }

                        buffer.AppendFormat(
                            "Error Registering COM pseudo-server '{0}': {1}",
                            endpoint.ComIdentity.ProgId,
                            exception.Message);
                    }
                }

                // display warning.
                if (buffer.Length > 0) {
                    MessageBox.Show(
                        buffer.ToString(),
                        "Endpoint Import Errors",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
                }

                ServersCTRL.Initialize(m_configuration);
            } catch (Exception exception) {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#5
0
        private void EditMI_Click(object sender, EventArgs e)
        {
            try
            {
                if (ItemsLV.SelectedItems.Count != 1)
                {
                    return;
                }

                ValueState state = ItemsLV.SelectedItems[0].Tag as ValueState;

                if (!IsEditableType(state.Component))
                {
                    return;
                }

                object value = null;

                /*
                 * object value = new SimpleValueEditDlg().ShowDialog(state.Component, state.Component.GetType());
                 *
                 * if (value == null)
                 * {
                 *  return;
                 * }
                 * */

                if (state.Value is IEncodeable)
                {
                    PropertyInfo property = (PropertyInfo)state.ComponentId;

                    MethodInfo[] accessors = property.GetAccessors();

                    for (int ii = 0; ii < accessors.Length; ii++)
                    {
                        if (accessors[ii].ReturnType == typeof(void))
                        {
                            accessors[ii].Invoke(state.Value, new object[] { value });
                            state.Component = value;
                            break;
                        }
                    }
                }

                DataValue datavalue = state.Value as DataValue;

                if (datavalue != null)
                {
                    int component = (int)state.ComponentId;

                    switch (component)
                    {
                    case 0: { datavalue.Value = value; break; }
                    }
                }

                if (state.Value is IList)
                {
                    int ii = (int)state.ComponentId;
                    ((IList)state.Value)[ii] = value;
                    state.Component          = value;
                }

                m_expanding = false;
                int  index     = 0;
                bool overwrite = true;
                ShowValue(ref index, ref overwrite, state.Value);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
示例#6
0
        private void NodesTV_MouseDown(object sender, MouseEventArgs e)
        {
            try {
                // selects the item that was right clicked on.
                TreeNode clickedNode = NodesTV.SelectedNode = NodesTV.GetNodeAt(e.X, e.Y);

                // no item clicked on - do nothing.
                if (clickedNode == null)
                {
                    return;
                }

                // start drag operation.
                if (e.Button == MouseButtons.Left)
                {
                    if (m_enableDragging)
                    {
                        object data = GetDataToDrag(clickedNode);

                        if (data != null)
                        {
                            NodesTV.DoDragDrop(data, DragDropEffects.Copy);
                        }
                    }

                    return;
                }

                // disable everything.
                if (NodesTV.ContextMenuStrip != null)
                {
                    foreach (ToolStripItem item in NodesTV.ContextMenuStrip.Items)
                    {
                        ToolStripMenuItem menuItem = item as ToolStripMenuItem;

                        if (menuItem == null)
                        {
                            continue;
                        }

                        menuItem.Enabled = false;

                        if (menuItem.DropDown != null)
                        {
                            foreach (ToolStripItem subItem in menuItem.DropDown.Items)
                            {
                                ToolStripMenuItem subMenuItem = subItem as ToolStripMenuItem;

                                if (subMenuItem != null)
                                {
                                    subMenuItem.Enabled = false;
                                }
                            }
                        }
                    }
                }

                // enable menu items according to context.
                if (e.Button == MouseButtons.Right)
                {
                    EnableMenuItems(clickedNode);
                }
            } catch (Exception exception) {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }