【问题标题】:Issue running batch file in a WinForms app在 WinForms 应用程序中运行批处理文件问题
【发布时间】:2019-01-06 03:53:43
【问题描述】:

作为程序的一部分,我需要运行一个批处理文件。
我正在做的人不希望任何人能够查看批处理脚本,所以我尝试将它包含在 WinForms 解决方案中,但我无法让它运行,我只是得到一个超时错误.

【问题讨论】:

标签: c# winforms batch-file


【解决方案1】:

迈克尔,

你可以用两种不同的方式来做。

  1. 在进程中提供脚本文件,但它会暴露你的代码,因为它在文件中打开。

  2. 您可以通过您的进程启动命令提示符并将您的代码作为参数提供。

    var p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/c mycmd.exe 2>&1";
    

    var p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = @"/c dir \windows";
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = false;
    p.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
    p.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
    p.Start();
    p.BeginErrorReadLine();
    p.BeginOutputReadLine();
    p.WaitForExit();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多