输出命令行(. bat文件)显示在文本框与c#

本文关键字:文本 显示 命令行 bat 文件 输出 | 更新日期: 2023-09-27 17:54:05

        Process proc = new Process();
        proc.StartInfo.FileName = @"C:'Users'Administrator'Desktop'Python27'ToolArtworkEmoji.bat";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        proc.OutputDataReceived += proc_OutputDataReceived;
        proc.BeginOutputReadLine();
        proc.WaitForExit();
        textbox1.Text = proc.StandardOutput.ReadToEnd();

为什么代码不激活。它不是在文本框中显示命令。我能帮忙吗?非常感谢

输出命令行(. bat文件)显示在文本框与c#

您需要在proc_OutputDataReceived处理程序中读取输出,而不是通过proc.StandardOutput.ReadToEnd();

换句话说,输入像

这样的东西
textbox1.BeginInvoke(new Action(()=>{textbox1.Text=e.Data;}));

proc_OutputDataReceived处理程序(e是一个DataReceivedEventArgs参数)

            private void button1_Click_1(object sender, EventArgs e)
    {
        Process proc = new Process();
        proc.StartInfo.FileName = @"C:'Users'Administrator'Desktop'Python27'ToolArtworkEmoji.bat";
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.Start();
        proc.OutputDataReceived += proc_OutputDataReceived;
        proc.BeginOutputReadLine();
        proc.WaitForExit();
    {
    private void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        tblog.BeginInvoke(new Action(() => { tblog.Text = e.Data; }));
    }

E已编辑。它不活跃。文本框不显示任何内容