以下是我的代码:
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。
我怎么知道它不起作用?
注意:我在静音模式下运行它,因此用户无法看到提示。在非静音模式下工作时会显示错误提示。
答案 0 :(得分:0)
在process.WaitForExit()
之后,您应该检查流程的退出代码。
Process.ExitCode
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx
如果全部成功,则为0
。如果失败则为非零。