C# 检查进程是否间接导致错误
我使用以下代码打开cmd并使用配置文件运行SQL Server setup.exeC# 检查进程是否间接导致错误,c#,sql-server,process,cmd,C#,Sql Server,Process,Cmd,我使用以下代码打开cmd并使用配置文件运行SQL Server setup.exe ProcessStartInfo pStartInfo = new ProcessStartInfo(); pStartInfo.FileName = "cmd.exe"; pStartInfo.Arguments = "/c /q setup.exe /c /q /configurationFile=../configurationFile
ProcessStartInfo pStartInfo = new ProcessStartInfo();
pStartInfo.FileName = "cmd.exe";
pStartInfo.Arguments = "/c /q setup.exe /c /q /configurationFile=../configurationFile.ini";
pStartInfo.WorkingDirectory = installDir + @"\SQL Server Unpacked";
pStartInfo.CreateNoWindow = true;
pStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pStartInfo.UseShellExecute = false;
pStartInfo.RedirectStandardInput = true;
pStartInfo.RedirectStandardOutput = true;
pStartInfo.RedirectStandardError = true;
Process p = new Process();
p.StartInfo = pStartInfo;
lb_SQLServerReadout.Text = "Please wait while the setup runs";
p.Start();
这工作正常。它将尝试使用配置文件的详细信息进行安装,如果配置文件包含无效的详细信息,则setup.exe将给出错误,并且不会安装
我希望能够获取错误代码并将其写入表单上的标签,这样用户就可以知道发生了错误以及为什么发生了错误。我考虑过使用Process.StandardError.ReadToEnd
,但它总是返回null
。我认为这是因为进程本身是cmd,而不是setup.exe(实际发生错误的地方)
我已经了解到,可以使用echo%errorlabel%
获取cmd中的最后一个错误代码,当我手动运行它时,它确实起作用,因此我尝试在代码中实现它:
p.StandardInput.WriteLine("echo %errorlevel%");
string s = p.StandardOutput.ReadLine();
p.WaitForExit();
我希望s
将被设置为echo%errorlevel%
输入的输出,但它只是被设置为null
如何通过cmd过程
获取SQL Server安装程序的错误代码
作为旁注,我考虑将setup.exe作为进程运行,但它需要提升,这意味着启用UseShellExecute
,这反过来意味着禁用RedirectStandardError
,这将阻止我获取错误代码。这完全不是必需的,是用cmd.exe/c
调用的“可执行文件”将是cmd.exe
本身的退出代码
您也可以在命令行上尝试此操作:
c:\> cmd.exe /c app.exe make-it-fail
c:\> echo %errorlevel%
第二行将打印“app.exe使其失败”的退出代码
因此,只需在Process.WaitForExit()之后读取Process.ExitCode
属性的值即可
我不知道(SQL Server的)setup.exe在失败的情况下设置了哪些特定的值。应该与0
不同(按惯例)