从vbscript获取错误输出

本文关键字:输出 取错误 获取 vbscript | 更新日期: 2023-09-27 18:05:54

我怎样才能正确地从c#中运行的vbscript得到错误输出?

下面是我的代码:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo("cscript");
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += (proc, outLine) => MessageBox.Show(outLine.Data, 
                                                            "Data:", 
                                                            MessageBoxButtons.OK, 
                                                            MessageBoxIcon.Information);
p.ErrorDataReceived += (proc, outLine) => MessageBox.Show(outLine.Data, 
                                                            "error!", 
                                                            MessageBoxButtons.OK, 
                                                            MessageBoxIcon.Error);
p.StartInfo.Arguments = "C:''test.vbs";
p.Start();
p.BeginOutputReadLine();  

这样我就可以从cscript中获得数据,但如果在脚本中有错误-进程只是关闭,没有消息…

从vbscript获取错误输出

哎呀。我的错——我忘了加

p.BeginErrorReadLine();

这就是答案