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

weixin_38065656 2017-11-01 05:59:22
我在用 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#语言,希望回帖的高手们留意一下,谢谢大家的帮助
...全文
20 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复

476

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧