gpt4 book ai didi

c# - 使用 BackgroundWorker.Backgroundworker 显示命令提示符输出

转载 作者:太空宇宙 更新时间:2023-11-03 16:23:51 26 4
gpt4 key购买 nike

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

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 输出发送到文本框或其他控件(实时),那也可以。

谢谢!

最佳答案

由于您将以下属性之一设置为 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

谢谢,
希望这对您有所帮助:)

关于c# - 使用 BackgroundWorker.Backgroundworker 显示命令提示符输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13150171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com