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

ImportCertificateToTrustBTN_Click() private method

private ImportCertificateToTrustBTN_Click ( object sender, EventArgs e ) : void
sender object
e System.EventArgs
return void
        private void ImportCertificateToTrustBTN_Click(object sender, EventArgs e)
        {
            try
            {
                const string caption = "Import Certificate";

                ManagedApplication application = ManageApplicationSecurityCTRL.GetSelectedApplication();

                if (application == null)
                {
                    return;
                }

                // load the configuration.
                application.Reload();

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

                // set current directory.
                if (m_currentDirectory == null)
                {
                    m_currentDirectory = Utils.GetAbsoluteDirectoryPath("%CommonApplicationData%\\OPC Foundation\\CertificateStores\\UA Applications", false, false);
                }

                if (m_currentDirectory == null)
                {
                    m_currentDirectory = new FileInfo(Application.ExecutablePath).DirectoryName;
                }

                // open file dialog.
                OpenFileDialog dialog = new OpenFileDialog();

                dialog.CheckFileExists = true;
                dialog.CheckPathExists = true;
                dialog.DefaultExt = ".der";
                dialog.Filter = "DER Files (*.der)|*.der|PKCS #12 Files (*.pfx)|*.pfx|All Files (*.*)|*.*";
                dialog.Multiselect = false;
                dialog.ValidateNames = true;
                dialog.Title = caption;
                dialog.FileName = null;
                dialog.InitialDirectory = m_currentDirectory;
                dialog.RestoreDirectory = true;

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

                FileInfo fileInfo = new FileInfo(dialog.FileName);
                m_currentDirectory = fileInfo.Directory.FullName;

                if (!fileInfo.Exists)
                {
                    return;
                }

                X509Certificate2 certificate = new X509Certificate2(fileInfo.FullName, (string)null, X509KeyStorageFlags.Exportable);
                ValidateAndImport(application.TrustList, certificate);
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }