1

I am creating this simple console application that will create my own certificate. Here's my code.

var fi = new FileInfo("certificate.cer");
if (!fi.Exists)
{
  var startInfo = new ProcessStartInfo();
  startInfo.FileName = "makecert.exe";
  startInfo.Arguments = "-sv SignRoot.pvk -cy authority -r sha1 -n \"CN=Certificate\" -ss my -sr localmachine certificate.cer";
  Process.Start(startInfo);
}
X509Certificate.CreateFromCertFile("certificate.cer");

But why am i getting this on my last line of code?

CryptographicException was unhandled.
Message=The system cannot find the file specified.
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
for-each
  • 619
  • 3
  • 7
  • 21

2 Answers2

4

You need to wait for the makecert process to exit before using the certificate.

 Process
    .Start(startInfo)
    .WaitForExit();
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

I resolved the situation by using a batch(.bat) file, because I find it easier. And let c# run the batch file when the program starts. Thanks for the help. :)

for-each
  • 619
  • 3
  • 7
  • 21