[英]How to know that an automated regsvr32 doesn't work
以下是我的代碼:
private static bool Register(string fullFileName, string username, SecureString password)
{
var isRegistered = false;
using (var process = new Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "regsvr32.exe";
startInfo.Arguments = string.Format("/s {0}", fullFileName);
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.ErrorDialog = true;
process.EnableRaisingEvents = true;
startInfo.Verb = "runas";
startInfo.UserName = username;
startInfo.Password = password;
try
{
isRegistered = process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message );
}
}
return isRegistered;
}
}
如果我要注釋掉以下內容:
startInfo.Verb = "runas"; startInfo.UserName = username; startInfo.Password = password;
那么它將無法正常運行,因為需要將其注冊為管理員。 但是isRegistered將設置為true。
我怎么知道它不起作用?
注意:我在靜默模式下運行它,因此用戶看不到提示。 在非靜默模式下顯示錯誤提示。
在process.WaitForExit()
,您應該檢查進程的退出代碼。
Process.ExitCode
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx
如果全部成功,則為0
。 如果失敗,則非零。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.