Opc.Ua.Configuration.MainForm.DeleteAllTrustedCertificatesBTN_Click C# (CSharp) Method

DeleteAllTrustedCertificatesBTN_Click() private method

private DeleteAllTrustedCertificatesBTN_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void DeleteAllTrustedCertificatesBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Delete All Trusted Certificates";

                ManagedApplication application = ManageApplicationSecurityCTRL.GetSelectedApplication();

                if (application == null)
                {
                    return;
                }

                if (application.TrustList == null)
                {
                    MessageBox.Show(application.ToString() + " does not have a trust list defined.", caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                StringBuilder buffer = new StringBuilder();

                buffer.Append("Deleting all certificates from a trust list will affect other applications if you are using shared trust lists.");
                buffer.Append("\r\n\r\n");
                buffer.Append("Are you sure you would like to delete all certificates from '");
                buffer.Append(application.TrustList.ToString());
                buffer.Append("'?");

                if (new YesNoDlg().ShowDialog(buffer.ToString(), caption) != DialogResult.Yes)
                {
                    return;
                }

                bool privateKeys = false;
                int count = 0;
                ICertificateStore store = application.TrustList.OpenStore();

                try
                {
                    foreach (X509Certificate2 certificate in store.Enumerate())
                    {
                        if (certificate.HasPrivateKey)
                        {
                            privateKeys = true;
                            continue;
                        }

                        store.Delete(certificate.Thumbprint);
                        count++;
                    }
                }
                finally
                {
                    store.Close();
                }

                MessageBox.Show(
                    this,
                    count.ToString() + " certificates deleted.",
                    caption,  
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Information);

                if (privateKeys)
                {
                    buffer = new StringBuilder();

                    buffer.Append("Some certificates were not deleted because they have private keys.");
                    buffer.Append("\r\n\r\n");
                    buffer.Append("Would you like to view them?");

                    if (new YesNoDlg().ShowDialog(buffer.ToString(), caption) != DialogResult.Yes)
                    {
                        return;
                    }

                    new CertificateListDlg().ShowDialog(application.TrustList, false);
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }