我在用 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#语言,希望回帖的高手们留意一下,谢谢大家的帮助