在WinForms应用中发布运行批处理文件

时间:2019-01-06 03:53:43

标签: c# winforms batch-file

作为程序的一部分,我需要运行一个批处理文件。
我要执行此操作的人员不希望任何人都能查看批处理脚本,因此我尝试将其包含在WinForms解决方案中,但是我无法使其运行,只是出现超时错误

1 个答案:

答案 0 :(得分:2)

迈克尔

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

  1. 要在Process中提供脚本文件,但它将在文件中打开时公开您的代码。

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

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

OR

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