Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将批处理文件输出重定向到winform文本框问题_C#_Winforms_Batch File - Fatal编程技术网

C# 将批处理文件输出重定向到winform文本框问题

C# 将批处理文件输出重定向到winform文本框问题,c#,winforms,batch-file,C#,Winforms,Batch File,我创建了一个GUI,它调用批处理文件,并在单击按钮后在文本框中显示命令行输出。批处理文件运行,输出被正确重定向一段时间,直到大约80行,文本框输出突然开始显示批处理文件中的实际代码。这是否表明批处理文件中存在错误或脚本存在问题?我真的不知道如何开始调试这个问题 我还注意到,我从GUI调用的批处理文件会调用其他批处理文件。这是否也会造成问题 我还应该提到,批处理文件成功地从命令行运行 private void buildProg_Click(object sender, EventArgs e)

我创建了一个GUI,它调用批处理文件,并在单击按钮后在文本框中显示命令行输出。批处理文件运行,输出被正确重定向一段时间,直到大约80行,文本框输出突然开始显示批处理文件中的实际代码。这是否表明批处理文件中存在错误或脚本存在问题?我真的不知道如何开始调试这个问题

我还注意到,我从GUI调用的批处理文件会调用其他批处理文件。这是否也会造成问题

我还应该提到,批处理文件成功地从命令行运行

private void buildProg_Click(object sender, EventArgs e)
{
    using (Process process = new Process())
    {
        process.StartInfo.WorkingDirectory = some_directory;
        process.StartInfo.FileName = "start.bat";
        process.StartInfo.Arguments = "arg1 arg2";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardError = true;
        process.OutputDataReceived += proc_OutputDataReceived;
        process.EnableRaisingEvents = true;
        process.Start();
        process.BeginOutputReadLine();
    }
}

private void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    this.Invoke((Action)(() =>
    {
        textBox1.AppendText(Environment.NewLine + e.Data);
    }));
}
当我从GUI运行它时,批处理文件似乎在这一部分出错

如果存在%version\u file\u path%(

set/p\u version=这里发生了两件事

1)
/p
语句请求用户输入,暂停命令并等待

2) 您正在事件触发之前处理进程,这意味着我们将无法模拟它继续执行所需的输入

这基本上解决了这个问题:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.FormClosing += Form1_FormClosing;
    }

    Process p;

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        p?.Dispose();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (p != null)
            p.Dispose();

        p = new Process();
        p.StartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        p.StartInfo.FileName = "test.bat";
        p.StartInfo.Arguments = "";
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = true;
        p.OutputDataReceived += proc_OutputDataReceived;
        p.Start();
        p.BeginOutputReadLine();
    }

    private void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        this.Invoke((Action)(() =>
        {
            textBox1.AppendText(Environment.NewLine + e.Data);

        }));

        //can use either of these lines.
        (sender as Process)?.StandardInput.WriteLine();
        //p.StandardInput.WriteLine();
    }
}

我想你是对的,这可能是一个角色的问题。。。我添加了批处理文件中出现问题的部分。我不允许修改批处理文件,因此是否有办法在保留批处理文件的同时绕过此部分?在重现您的问题后,我可以看到是
/p
开关导致OutputDataReceived事件停止触发。这确实是一个非常奇怪的错误。如果我能找到答案,我会更新我的答案。。。但这可能是我无法理解的,就是这样!很高兴看到它工作-谢谢你的帮助!!