【发布时间】:2017-05-19 09:24:26
【问题描述】:
我正在尝试订阅 process.OutputDataReceived。看不到任何结果(事件未在输出中触发)我按照Msdn 上的示例进行操作。但是process.BeginOutputReadLine();给了我一个例外。我试图运行的过程是一个简单的批处理文件命令wmic product where "Name like '%%Microsoft Visual C++ %%'" get Name, Version 奇怪的是process.Exited 事件工作得很好。
这里是代码
void DoSomething(object sendingProcess, DataReceivedEventArgs outLine)
{
Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = outLine.Data; }));
}
public MainWindow()
{
InitializeComponent();
string batPath = @"..\..\WMIC batch\";
var process = new Process();
process.StartInfo.WorkingDirectory = batPath;
process.StartInfo.FileName = "Redistributable_Packages_Check.bat";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler(Process_Exited);
process.OutputDataReceived +=new DataReceivedEventHandler(DoSomething);
process.StartInfo.RedirectStandardInput = true;
process.Start();
StreamWriter sortStreamWriter = process.StandardInput;
process.BeginOutputReadLine();
//textBox.Text = "Initialised";
}
private void Process_Exited(object sender, EventArgs e)
{
Application.Current.Dispatcher.Invoke(new Action(() => { textBox.Text = "Process has exited"; }));
}
异常详情{“系统找不到指定的文件”}
P.S 省略行
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
触发批处理文件。所以我相信它可以找到文件。
【问题讨论】:
-
当你尝试在 cmd 中运行命令时会发生什么?
-
尝试指定批处理文件的绝对路径:Process.Start("c:\yourfolder\WMIC batch\Redistributable_Packages_Check.bat");
-
@C0d1ngJammer 在 cmd 中运行命令时,会显示系统上安装的 Microsoft 软件包。因此,我的系统 30 个结果中的前 4 个结果是:Microsoft Visual C++ 2008 Redistributable - x86 9.0.30729.4974 9.0.30729.4974,Microsoft Visual C++ 2013 x86-x64 Compilers 12.0.21005,Microsoft Visual C++ 2015 x64 Debug Runtime - 14.0.24215 14.0.24215,Microsoft Visual C++ 2010 x64 Redistributable - 10.0.40219 10.0.40219,