2

我有一个使用异步后台工作程序执行批处理文件的程序。这是代码:

public static void Execute(CmdObj obj, bool batch)
{
    CmdObj = obj;

    var theWorker = new BackgroundWorker();
    theWorker.RunWorkerCompleted += WorkerCompleted;
    theWorker.WorkerReportsProgress = true;

    if(batch)
    {
        theWorker.DoWork += WorkerBatchWork;
    }else{
        theWorker.DoWork += WorkerCommandWork;
    }
    theWorker.RunWorkerAsync();

}

private static void WorkerBatchWork(object sender, DoWorkEventArgs e)
{
    RunBatch(CmdObj);
}

private static void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    var temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    if (temp != null)
        ProgramPath = temp.Substring(6);

    WriteLog(CmdObj.Activity, false);
    WriteLog(CmdObj.Error, true);

    CmdObj.TheButton.Enabled = true;
}

private static void RunBatch(CmdObj obj)
{
    var process = new Process();
    var startInfo = new ProcessStartInfo
                        {
                            FileName = obj.SrcFile,
                            WindowStyle = ProcessWindowStyle.Normal,
                            CreateNoWindow = false,
                            RedirectStandardInput = true,
                            RedirectStandardOutput = false,
                            RedirectStandardError = true,
                            UseShellExecute = false
                        };


    try
    {
        if (!obj.SrcFile.ToLower().Trim().EndsWith(".bat"))
            throw new FileLoadException("Not a valid .bat file",obj.SrcFile);

        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();

        //obj.Activity = process.StandardOutput.ReadToEnd();
        obj.Error = process.StandardError.ReadToEnd();

    }
    catch (Exception ex)
    {
        obj.Exception = ex;
    }
    finally
    {
        process.Close();
    }
}

class CmdObj
{
    public string Command { get; set; }
    public string SrcFile { get; set; }
    public string Activity { get; set; }
    public string Error { get; set; }
    public Exception Exception { get; set; }
    public Button TheButton { get; set; }
}

所以当我运行这个程序并选择一个批处理文件来执行时,我得到一个空白的 CMD 窗口。当我的程序执行批处理文件时,有什么方法可以在 CMD 窗口中显示输出吗?或者,如果我可以将 CMD 输出发送到文本框或其他控件(实时),那也可以。

谢谢!

4

2 回答 2

0

由于您将以下属性之一设置为true,您将获得一个空白的黑色窗口

RedirectStandardInput
RedirectStandardOutput
RedirectStandardError 

这实际上会发生,因为您已经告诉您的应用程序从目标进程接收/发送输出/输入

我看到您正在尝试使用此行从批处理文件中获取输出

//obj.Activity = process.StandardOutput.ReadToEnd();

RedirectStandardOutput不幸的是,除非您设置为这样,否则这将不起作用,True以便您的应用程序能够读取目标批处理文件的输出。否则,输出将为空

例子

private void RunBatch(CmdObj obj)
{
    var process = new Process();
    var startInfo = new ProcessStartInfo
    {
        FileName = obj.SrcFile,
        WindowStyle = ProcessWindowStyle.Normal,
        CreateNoWindow = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true, //This is required if we want to get the output
        RedirectStandardError = true,
        UseShellExecute = false
    };


    try
    {

        if (!obj.SrcFile.ToLower().Trim().EndsWith(".bat"))
        throw new FileLoadException("Not a valid .bat file",obj.SrcFile);

        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();

        obj.Activity = process.StandardOutput.ReadToEnd(); //Get the output from the target process
        obj.Error = process.StandardError.ReadToEnd();

    }
    catch (Exception ex)
    {
        obj.Exception = ex;
    }
    finally
    {
        process.Close(); //Terminate the process after getting what is required
    }
}

注意:UseShellExecute必须是False为了使用RedirectStandardOutput

谢谢,
我希望你觉得这有帮助:)

于 2012-10-31T03:18:52.717 回答
0

你不需要打电话吗

 process.BeginErrorReadLine();
 process.BeginOutputReadLine();

进程开始后?如果我没记错的话,如果没有这个,我会遇到一些问题。

于 2017-06-06T09:31:42.573 回答