void initCertCombos()
        {
            X509Certificate2 certSelected = null;
            X509Store        x509Store    = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            System.IdentityModel.Tokens.X509SecurityToken securityToken;

            x509Store.Open(OpenFlags.ReadOnly);
            String storedClientCertSerial       = ConfigurationManager.AppSettings["ApotekCertSerialNo"];
            String storedrfEncryptionCertSerial = ConfigurationManager.AppSettings["ReseptformidlerCertSerialNo"];


            X509Certificate2Collection col = x509Store.Certificates;

            foreach (X509Certificate2 cert in col)
            {
                Sertifikat s = new Sertifikat(cert);
                if (cert.HasPrivateKey == true)
                {
                    cmbCertClient.Items.Add(s);
                    if (s.cert.SerialNumber == storedClientCertSerial)
                    {
                        cmbCertClient.SelectedItem = s;
                    }
                }
                if (cert.Subject.Contains("Reseptformidleren") || cert.Subject.Contains("RESEPTFORMIDLEREN"))
                {
                    cmbCertServer.Items.Add(s);
                    if (s.cert.SerialNumber == storedrfEncryptionCertSerial)
                    {
                        cmbCertServer.SelectedItem = s;
                    }
                }
            }
            if (cmbCertServer.SelectedIndex < 0 && cmbCertServer.Items.Count > 0)
            {
                cmbCertServer.SelectedIndex = 0;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            // Save the current settings:

            try
            {
                Configuration configuration =
                    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configuration.AppSettings.Settings["customDatakatalog"].Value = rbCustom.Checked ? "true" : "false";
                configuration.AppSettings.Settings["datakatalog"].Value       = tbFolder.Text;
                configuration.AppSettings.Settings["defaultKundeId"].Value    = tbKundeid.Text;
                Sertifikat selectedCert = (Sertifikat)cmbCertClient.SelectedItem;
                if (selectedCert != null)
                {
                    configuration.AppSettings.Settings["ApotekCertSerialNo"].Value = selectedCert.cert.SerialNumber;
                    int length = selectedCert.cert.SerialNumber.Length;
                }
                selectedCert = (Sertifikat)cmbCertServer.SelectedItem;
                if (selectedCert != null)
                {
                    configuration.AppSettings.Settings["ReseptformidlerCertSerialNo"].Value = selectedCert.cert.SerialNumber;
                }

                configuration.Save(ConfigurationSaveMode.Full, true);
                ConfigurationManager.RefreshSection("appSettings");

                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
                DialogResult = System.Windows.Forms.DialogResult.Abort;
            }

            Close();
        }