예제 #1
0
 public static string TryCreate(string fileName, out CertFileItem result)
 {
     try
     {
         FileInfo file = new FileInfo(fileName);
         if (!file.Exists)
         {
             throw new FileNotFoundException("File not found", fileName);
         }
         if (file.Length > int.MaxValue)
         {
             throw new ArgumentException("File too large", "fileName");
         }
         byte[] data = new byte[(int)file.Length];
         using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read))
         {
             if (stream.Read(data, 0, data.Length) != data.Length)
             {
                 throw new IOException("Not all data retrieved from file");
             }
         }
         result = new CertFileItem(new X509Certificate2(data), file);
         return(null);
     }
     catch (Exception exc)
     {
         result = null;
         return("Error loading certificate from file: " + exc.Message);
     }
 }
예제 #2
0
 private void openCertFileButton_Click(object sender, EventArgs e)
 {
     if (openCertificateDialog.ShowDialog(this) == DialogResult.OK)
     {
         string fileName = openCertificateDialog.FileName;
         if (null == (fileName = CertFileItem.TryCreate(fileName, out CertFileItem result)))
         {
             SelectedCert = result;
         }
         else
         {
             MessageBox.Show(this, fileName, "Load Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
     }
 }