C# 捕获标准误差

C# 捕获标准误差,c#,standard-error,C#,Standard Error,我正在遵循其他SO文章关于这个主题的指导,但我一直无法找到我需要的东西。我有以下代码块,在本例中,它启动一个已知的已损坏的.msi,以测试标准错误消息的检索: ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c " + thirdPartyApp.Item2.InstallString); procStartInfo.RedirectStandardOutput = true; procStartInfo.Re

我正在遵循其他SO文章关于这个主题的指导,但我一直无法找到我需要的东西。我有以下代码块,在本例中,它启动一个已知的已损坏的.msi,以测试标准错误消息的检索:

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c " + thirdPartyApp.Item2.InstallString);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
writeEvent(EventLogEntryType.Information, "Attempting to launch upgrade: " + thirdPartyApp.Item2.InstallString, "Third Party Update");
proc.Start();
string stderror = proc.StandardError.ReadToEnd();
proc.WaitForExit();
手动启动时,此.msi抛出错误代码1620,并显示消息“无法打开此安装包”。请与应用程序供应商联系,以验证这是一个有效的Windows Installer软件包

在调试时,如果我查看proc.ExitCode,它的值是1620——因此它正确地捕获了这个值。我添加了行字符串stderror=proc.StandardError.ReadToEnd;尝试捕获错误文本,但在进程执行并出错后,stderror的值为


如何将实际的错误文本抓取到变量中?

您确定proc甚至会写入StdErr吗?我的意思是,它是一个窗口应用程序,向控制台写入数据是不可能实现的。相反,他们只是解析错误代码来显示一个消息框。我已经编辑了你的标题。请看,如果一致意见是否定的,他们就不应该这样做。您可以使用Try/Catch块来捕获错误吗?我相信你要找的文本会在前消息中。我必须承认,我不知道proc.start的结果会是什么,但是值得一试吗?尝试了try/Catch块,但是错误的安装文件实际上不会导致异常。因此我无法捕获异常消息。请尝试使用msiexec.exe而不是cmd.exe。