我正在运行一个 7za.exe 进程来 7zip 一个这样的文件:
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = zip_Path;
proc.Arguments = "a -t7z ziped.7z file.bak";
proc.RedirectStandardInput = true;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
p = Process.Start(proc);
while (!p.HasExited)
{
res = p.StandardOutput.ReadLine();
texto += "\n" + res;
}
MessageBox.Show(texto + "\n" + "ErrorCode:" + p.ExitCode);
p.Close();
这工作正常,但是当我在控制台上手动运行 7za.exe
时,我可以看到压缩进度。有什么办法可以在我的应用程序上实时输出吗?
最佳答案
我知道这是一个老问题,但我在这里找到了答案 https://stackoverflow.com/a/4291965/4050735
答案中的代码示例:
var proc = new Process {
StartInfo = new ProcessStartInfo {
FileName = "program.exe",
Arguments = "command line arguments to your executable",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
string line = proc.StandardOutput.ReadLine();
// do something with line
}
我发现这会返回您通常会在控制台中看到的每一行,例如一次压缩一个文件的每个文件。
关于c# 7za.exe 进程状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10552544/