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

标签 c# winforms batch-file

我创建了一个 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 运行它时,批处理文件似乎在这部分出错了。

if exist %version_file_path% ( set /p _version= <%version_file_path% ) else ( echo %version_file_path% not found pause )

最佳答案

这里发生了两件事;

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();
    }
}

关于c# - 将批处理文件输出重定向到 winform 文本框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44851533/

相关文章:

c# - C#中的.wav到.flac转换器

c# - 在 ListView 控件中隐藏 ID 列

c# - 可以使用 Control.Invoke 而不是使用锁吗?

winforms - Windows 窗体设计和增强可用性的资源

batch-file - Win Batch - IF EXIST %date% 在文件名搜索中 - 不起作用

batch-file - 按顺序运行多个命令

c# - 使用 C# 对 Excel 中的列进行排序

c# - 错误 : An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib. dll

c# - 删除 div 之间的间距,bootstrap

batch-file - 需要从 1 行很长的文本文件中替换 13 个空格