C# - 捕获 Windows 应用程序输出
我的应用程序启动“C:\Windows\System32\Msra.Exe”来控制域计算机。有没有办法捕获此 msra.Exe 显示的错误消息。 (即来自 msra.exe 的内部错误消息,而不是来自我的应用程序的错误消息。) 该应用程序本身是一个 Windows 窗体应用程序。
任何帮助表示赞赏。
启动 MSRA 的代码如下...它只是完整应用程序的一个片段。
string msra = "C:\\Windows\\System32\\runas.exe";
string domainname = "**********";
string domaincontroller = "*************";
if (File.Exists(msra) == false)
{
System.Windows.Forms.MessageBox.Show("Runas.exe not found.\n\rPlease contact your internal IT support.", "Fatal Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.ErrorDialog = true;
p.StartInfo.FileName = msra;
p.StartInfo.Arguments = "/noprofile /netonly /user:" + domainname + "\\" + username + " \"cmd /server:" + domaincontroller + " /C msra.exe /offerra " + computerip + "\"";
p.Start();
p.Dispose();
Thread.Sleep(1700);
SendKeys.SendWait(password);
SendKeys.SendWait("{ENTER}");
}
catch
{
System.Windows.Forms.MessageBox.Show("MSRA could not be started for an unknown reason");
}
}
My application launches "C:\Windows\System32\Msra.Exe" to control a domaincomputer. Is there a way I can capture the error messages that this msra.Exe shows. (I.e. internal error messages from the msra.exe and not the ones from my app.)
The app itself is a windows Forms app.
Any help is appreciated.
The Code to start MSRA is below... it is just a snippet of the complete application.
string msra = "C:\\Windows\\System32\\runas.exe";
string domainname = "**********";
string domaincontroller = "*************";
if (File.Exists(msra) == false)
{
System.Windows.Forms.MessageBox.Show("Runas.exe not found.\n\rPlease contact your internal IT support.", "Fatal Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
}
else
{
try
{
Process p = new Process();
p.StartInfo.UseShellExecute = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.StartInfo.ErrorDialog = true;
p.StartInfo.FileName = msra;
p.StartInfo.Arguments = "/noprofile /netonly /user:" + domainname + "\\" + username + " \"cmd /server:" + domaincontroller + " /C msra.exe /offerra " + computerip + "\"";
p.Start();
p.Dispose();
Thread.Sleep(1700);
SendKeys.SendWait(password);
SendKeys.SendWait("{ENTER}");
}
catch
{
System.Windows.Forms.MessageBox.Show("MSRA could not be started for an unknown reason");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将
RedirectStandardOutput
或RedirectStandardError
设置为true
以便能够从进程的标准输出或错误输出中读取。然后,您可以选择如何实际读取数据:
StandardOutput
属性OutputDataReceived
事件并调用BeginOutputReadLine()
或相应的成员对于错误流。
You can set
RedirectStandardOutput
orRedirectStandardError
totrue
to be able to read from the standard output or error output of the process.You then have several options how to actually read the data:
StandardOutput
propertyOutputDataReceived
event and callBeginOutputReadLine()
Or the corresponding members for the error stream.
您需要 http://msdn.microsoft.com/ en-us/library/system.diagnostics.process.standardoutput.aspx
[更新为指向 .net 示例]
You need to http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx
[Updated to point to a .net example]
您正在使用 Process,因此请尝试 Process.StandardError 属性。您为其分配一个流并且您将能够使用它。
http://msdn.microsoft.com/en-us /library/system.diagnostics.process.standarderror.aspx
当您在那里时,您还可以使用 Process.StandardOutput
http://msdn.microsoft.com/en-us/library /system.diagnostics.process.standardoutput.aspx
You are using Process so try the Process.StandardError property. You assign it a stream and you will be able to use it.
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx
And while you're there you can also use Process.StandardOutput
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx