private void ListCurrentCertificates(ListBox listBox, Regex regex) { CertLocation certLocation = (CertLocation)listBox.Tag; listBox.Items.Clear(); X509Store store = null; try { store = new X509Store(certLocation.StoreName, certLocation.StoreLocation); store.Open(OpenFlags.ReadOnly); foreach (X509Certificate2 cert in store.Certificates) { if (regex == null || IsRegexMatch(regex, cert)) { listBox.Items.Add(new CertData(cert.RawData)); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (store != null) { store.Close(); } } }
private void InstallCertificate(ListBox listBox, CertLocation certLocation, string name) { X509Store store = null; try { store = new X509Store(certLocation.StoreName, certLocation.StoreLocation); store.Open(OpenFlags.ReadWrite); X509Certificate2 cert = new X509Certificate2(); cert.Import(name, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); store.Add(cert); listBox.Items.Insert(0, new CertData(cert.RawData)); } catch (Exception ex) { MessageBox.Show(ex.Message, "Installation problem", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { if (store != null) { store.Close(); } } }
private static void listBoxInstalledCerts_MouseDown(object sender, MouseEventArgs e) { ListBox senderListbox = sender as ListBox; if (senderListbox == null) { return; } int draggedIndex = senderListbox.IndexFromPoint(e.X, e.Y); if (draggedIndex == -1) { return; } CertLocation loc = senderListbox.Tag as CertLocation; List <string> exportedFilenames = new List <string>(); string exportFolder = Path.GetTempPath(); // Environment.GetFolderPath(Environment.SpecialFolder.Desktop); foreach (CertData certData in senderListbox.SelectedItems) { string exportedFilename = CertificateUtilMainForm.ExportCertToFolder(certData, exportFolder, "exported"); exportedFilenames.Add(exportedFilename); } DataObject dto = new DataObject( DataFormats.FileDrop, exportedFilenames.ToArray()); senderListbox.DoDragDrop(dto, DragDropEffects.Move); }
private void SetupListbox(ListBox listBox, CertLocation certLocation) { Debug.Assert(null != listBox, "null != listBox"); Debug.Assert(null != certLocation, "null != certLocation"); listBox.Tag = certLocation; listBox.HorizontalScrollbar = true; listBox.AllowDrop = true; listBox.SelectionMode = SelectionMode.MultiExtended; listBox.DragDrop += new DragEventHandler(this.listBox_DragDrop); listBox.DragEnter += new DragEventHandler(this.listBox_DragEnter); listBox.DragEnter += new DragEventHandler(this.listBox_DragEnterColoring); listBox.DragLeave += new EventHandler(listBox_DragLeaveColoring); listBox.MouseDown += new MouseEventHandler(listBoxInstalledCerts_MouseDown); ListCurrentCertificates(listBox, null); }
private void listBox_DragEnter(object sender, DragEventArgs e) { ListBox listBox = sender as ListBox; CertLocation loc = listBox.Tag as CertLocation; // listBox.Items.Add(loc.ToString()); if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] names = (string[])e.Data.GetData(DataFormats.FileDrop, false); if (ContainsCertificateFiles(names)) { e.Effect = DragDropEffects.Copy; return; } } e.Effect = DragDropEffects.None; }
private void listBox_DragDrop(object sender, DragEventArgs e) { ListBox listBox = sender as ListBox; CertLocation loc = listBox.Tag as CertLocation; string[] names = (string[])e.Data.GetData(DataFormats.FileDrop, false); foreach (string name in names) { if (IsCertificateFile(name)) { try { InstallCertificate(listBox, loc, name); } catch (CryptographicException cex) { MessageBox.Show(cex.Message, "Cryptography problem", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } listBox_DragLeaveColoring(sender, e); }