使用BackgroundWorker.Backgroundworker显示命令提示输出 [英] Show Command Prompt Output with BackgroundWorker.Backgroundworker
问题描述
我有一个程序使用异步后台工作程序执行批处理文件。下面是代码:
I have a program that executes a batch file using an asynchronous background worker. Here is the code:
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输出发送到文本框或一些其他控件(实时),这也将工作。
So when I run this program and choose a batch file to execute, I get a blank CMD window. Is there some way to show the output in the CMD window when my program executes the batch file? Alternatively, if I could have the CMD output sent to a textbox or some other control (in real-time), that would work as well.
谢谢! >
Thanks!
推荐答案
由于您将以下属性之一设置为 true
获得空白的黑色窗口
Since you have one of the following properties set to true
, you'll get a blank black Window
RedirectStandardInput
RedirectStandardOutput
RedirectStandardError
这实际上是因为你已经告诉你的应用程序从目标进程接收/发送输出/输入
This will actually happen because you've told your application to receive/send the output/input from the target process
我看到您尝试使用此行获取批处理文件的输出
I see that you were trying to get the output from the batch file using this line
//obj.Activity = process.StandardOutput.ReadToEnd();
$ b < $ c>到 True
,以便您的应用程序能够读取目标批处理文件的输出。否则,输出将为空。
Unfortunately, this won't work unless you set RedirectStandardOutput
to True
so that your application would have the ability to read the output from the target batch file. Otherwise, the output will be empty
示例
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
}
}
strong>注意,为了使用 RedirectStandardOutput,请注意:
UseShellExecute
必须 False
Notice that: UseShellExecute
must be False
in order to use RedirectStandardOutput
感谢您,
希望您觉得这项资讯有帮助:)
Thanks,
I hope you find this helpful :)
这篇关于使用BackgroundWorker.Backgroundworker显示命令提示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!