如何知道自动 regsvr32 不起作用
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;
}
}
- 例如用户名和密码错误,它会进入catch部分,我可以return false - FINE
如果我注释掉以下内容:
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
。如果失败则为非零。
以下是我的代码:
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;
}
}
- 例如用户名和密码错误,它会进入catch部分,我可以return false - FINE
如果我注释掉以下内容:
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
。如果失败则为非零。