重定向的输出未显示在文本框中

时间:2019-06-07 18:20:32

标签: c# winforms cmd

我是Visual Studio的新手。我想编写一个打开“ cmd.exe”的Windows窗体应用程序,用户可以在文本框中输入命令。 cmd的输出消息应重定向到另一个文本框。由于某些原因,输出消息未显示在文本框中

我尝试谷歌搜索答案,但似乎没有一个对我有用。非常感谢有人可以帮助我确定问题

        public cmd_helper()
        {
            //Initialize
            InitializeComponent();
            Load_Cmd_String();
            updata_box();
        }

        private void Start_button_Click(object sender, EventArgs e)
        {
            myProcess.StartInfo = new ProcessStartInfo("cmd.exe")
            {
                RedirectStandardInput = true,
                UseShellExecute = false,
                WorkingDirectory = @"C:\",
                CreateNoWindow = true,
                Verb = "runas"

            };

            // event handlers for output & error
            myProcess.OutputDataReceived += myProcess_OutputDataReceived;
            myProcess.ErrorDataReceived += myProcess_ErrorDataReceived;

            // start process
            myProcess.Start();
            cmd_output_text.AppendText("cmd running...\n");
        }

        private void Close_buttom_Click(object sender, EventArgs e)
        {
            myProcess.Close();
            cmd_output_text.AppendText("cmd closed...\n");
        }

        private void myProcess_ErrorDataReceived(object sender, 
             DataReceivedEventArgs e)
        {
            Process myProcess = sender as Process;
            if (myProcess == null)
                return;
            cmd_output_text.AppendText(e.Data);
        }

        private void myProcess_OutputDataReceived(object sender, 
             DataReceivedEventArgs e)
        {
            Process myProcess = sender as Process;
            if (myProcess == null)
                return;
            cmd_output_text.AppendText(e.Data);
        }

        private void Send_cmd_button_Click(object sender, EventArgs e)
        {
         myProcess.StandardInput.Write(command[name.IndexOf(command_box.Text)] + myProcess.StandardInput.NewLine);
        }

注意:    我有两个按钮,分别是“开始”和“停止”,它们将打开和关闭cmd.exe。   用户应该键入类似以下内容:dir,cd到某个目录,cd到另一个目录

2 个答案:

答案 0 :(得分:0)

public static void Main()
{
      var p = new Process();  
      p.StartInfo.UseShellExecute = false;  
      p.StartInfo.RedirectStandardOutput = true;  
      p.StartInfo.FileName = "Write500Lines.exe";  
      p.Start();  

      // To avoid deadlocks, always read the output stream first and then wait.  
      string output = p.StandardOutput.ReadToEnd();  
      p.WaitForExit();

      Console.WriteLine(output);
}

答案 1 :(得分:0)

尝试一下。

private void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Process myProcess = sender as Process;
    if (myProcess == null)
        return;
    cmd_output_text.Invoke(new Action (delegate { cmd_output_text.AppendText("\n"+e.Data); }));
}

此外,您需要添加此行“ myProcess.BeginOutputReadLine();”到您的代码

private void Start_button_Click(object sender, EventArgs e)
    {
        myProcess = new Process();
        myProcess.StartInfo = new ProcessStartInfo("cmd.exe")
        {
            RedirectStandardInput = true,
            UseShellExecute = false,
            RedirectStandardError=true,
            RedirectStandardOutput = true,
            WorkingDirectory = @"C:\",
            CreateNoWindow = true,
            Verb = "runas"

        };

        // event handlers for output & error
        myProcess.OutputDataReceived += myProcess_OutputDataReceived;
        myProcess.ErrorDataReceived += myProcess_ErrorDataReceived;

        // start process
        myProcess.Start();
        **myProcess.BeginOutputReadLine();**
        cmd_output_text.AppendText("cmd running...\n");
    }

希望它对你有帮助!