2013-02-06 19 views
1

我正在創建這個將創建我自己的證書的簡單控制檯應用程序。這是我的代碼。在創建證書時未處理的密碼異常

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"); 

但是爲什麼我在最後一行代碼中得到這個?

CryptographicException was unhandled. 
Message=The system cannot find the file specified. 

回答

0

我通過使用批處理(.bat)文件解決了這種情況,因爲我覺得它更容易。讓c#在程序啓動時運行批處理文件。謝謝您的幫助。 :)

4

在使用證書之前,您需要等待makecert進程退出。

Process 
    .Start(startInfo) 
    .WaitForExit(); 
+0

它沒有工作.. –