询问者
我用winform执行cmd命令,为什么得不到错误信息?

问题
-
我在用 nodejs 想在winform里直接执行 javascript,js里如 console.log('hello word');可以正常返回,可是一些错误信息,却不显示,只有在cmd窗口才显示:源码及截图如下:
private void BtnRun_Click(object sender, EventArgs e) { ExcuteDosCommand(TxtCommand.Text); } private void ExcuteDosCommand(string cmd) { try { Process p = new Process(); p.StartInfo.FileName = "cmd"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.OutputDataReceived += new DataReceivedEventHandler(sortProcess_OutputDataReceived); p.Start(); StreamWriter cmdWriter = p.StandardInput; p.BeginOutputReadLine(); if (!String.IsNullOrEmpty(cmd)) { cmdWriter.WriteLine(cmd); } cmdWriter.Close(); p.WaitForExit(); p.Close(); } catch(Exception ex) { MessageBox.Show("执行命令失败,请检查输入的命令是否正确!"); } } private void sortProcess_OutputDataReceived(object sender,DataReceivedEventArgs e) { if(!String.IsNullOrEmpty(e.Data)) { //this.BeginInvoke(new Action(() => { this.listBox1.Items.Add(e.Data); })); this.BeginInvoke(new Action(() => { this.TxtCmdShow.Text += e.Data +"\r\n"; })); } }
我是半路出家,而且是自学 学的是C#语言,希望回帖的高手们留意一下,谢谢大家的帮助
全部回复
-
Hi lkf18,你可以尝试下方法,通过p.StandardError读取错误,StandardOutput读取控制台输出信息
private void ExcuteDosCommand(string cmd) { try { Process p = new Process(); p.StartInfo.FileName = "cmd"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.OutputDataReceived += new DataReceivedEventHandler(sortProcess_OutputDataReceived); p.Start(); StreamWriter cmdWriter = p.StandardInput; p.BeginOutputReadLine(); if (!String.IsNullOrEmpty(cmd)) { cmdWriter.WriteLine(cmd); } cmdWriter.Close(); p.WaitForExit(); string stdoutx = p.StandardOutput.ReadToEnd(); string stderrx = p.StandardError.ReadToEnd(); Console.WriteLine("Exit code : {0}", p.ExitCode); Console.WriteLine("Stdout : {0}", stdoutx); Console.WriteLine("Stderr : {0}", stderrx); p.Close(); } catch (Exception ex) { MessageBox.Show("执行命令失败,请检查输入的命令是否正确!"); }
如果理解错误,您可以将Demo上传到OneDrive(包括您的测试材料node js 文件等)。 我们可以下载并进行调试。 这将有助于我们快速分析您的问题。
分享OneDrive文件和文件夹:
https://support.office.com/zh-cn/article/%e5%85%b1%e4%ba%ab-OneDrive-%e6%96%87%e4%bb%b6%e5%92%8c%e6%96%87%e4%bb%b6%e5%a4%b9-9fcc2f7d-de0c-4cec-93b0-a82024800c07?ui=zh-CN&rs=zh-CN&ad=CNBest Regards,
Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi lkf18,我下载你的Demo,运行时报下面的错误。
下面时关于如何获取Process相关信息的属性。Process.StandardOutput 属性:
https://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.standardoutput.aspxProcess.StandardError 属性:
https://msdn.microsoft.com/zh-cn/library/system.diagnostics.process.standarderror(v=vs.110).aspx
我想你的问题还是关于javascript脚本这块。
Best Regards,Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.