c#7za.exe进程状态
我正在运行一个7za.exe进程来对文件进行7zip压缩,如下所示:c#7za.exe进程状态,c#,process,progress,7zip,C#,Process,Progress,7zip,我正在运行一个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 = fa
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();
private static void CreateZip(string path, string zipFilename, Action<int> onProgress) {
Regex REX_SevenZipStatus = new Regex(@"(?<p>[0-9]+)%");
Process p = new Process();
p.StartInfo.FileName = "7za.exe";
p.StartInfo.Arguments = string.Format("a -y -r -bsp1 -bse1 -bso1 {0} {1}",
zipFilename, path);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += (sender, e) => {
if (onProgress != null) {
Match m = REX_SevenZipStatus.Match(e.Data ?? "");
if (m != null && m.Success) {
int procent = int.Parse(m.Groups["p"].Value);
onProgress(procent);
}
}
};
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
这很好,但是当我在控制台上手动运行7za.exe
时,我可以看到压缩过程。有没有办法在我的应用程序上实时输出它?而不是:
proc.CreateNoWindow = true;
尝试:
取决于它与控制台的交互方式,
ReadLine()
可能不够,因为它仅在输出新行字符时返回
7za.exe可能正在操纵当前行以显示进度,在这种情况下,您应该能够使用Read()
如果您想更好地了解压缩过程,可以使用LZMA C#SDK进行查看。我知道这是一个老问题,但我在这里找到了答案 答案中的代码示例:
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
}
我发现它返回了您通常在控制台中看到的每一行,例如每次压缩一个文件。嘿,我今天遇到了同样的问题。因此,对于所有搜索答案并访问此网站的人(我想,当你在2014年发布此问题时,你将不再需要此信息),以下是我如何解决此问题的: 其核心是7-Zip使用不同的流写入输出,而C#只获取其中一个流。但是您可以通过使用命令行参数强制7-Zip只使用一个流
-bsp1 -bse1 -bso1
除了你还需要什么。然后像这样捕获百分比部分:
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();
private static void CreateZip(string path, string zipFilename, Action<int> onProgress) {
Regex REX_SevenZipStatus = new Regex(@"(?<p>[0-9]+)%");
Process p = new Process();
p.StartInfo.FileName = "7za.exe";
p.StartInfo.Arguments = string.Format("a -y -r -bsp1 -bse1 -bso1 {0} {1}",
zipFilename, path);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += (sender, e) => {
if (onProgress != null) {
Match m = REX_SevenZipStatus.Match(e.Data ?? "");
if (m != null && m.Success) {
int procent = int.Parse(m.Groups["p"].Value);
onProgress(procent);
}
}
};
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
private static void CreateZip(字符串路径、字符串zipFilename、操作onProgress){
正则表达式REX_SevenZipStatus=新正则表达式(@“(?[0-9]+)%)”;
过程p=新过程();
p、 StartInfo.FileName=“7za.exe”;
p、 StartInfo.Arguments=string.Format(“a-y-r-bsp1-bse1-bso1{0}{1}”,
zipFilename,路径);
p、 StartInfo.UseShellExecute=false;
p、 StartInfo.RedirectStandardOutput=true;
p、 OutputDataReceived+=(发送方,e)=>{
if(onProgress!=null){
匹配m=REX_SevenZipStatus.Match(如数据??);
如果(m!=null&&m.Success){
int procent=int.Parse(m.Groups[“p”].Value);
进展(procent);
}
}
};
p、 Start();
p、 BeginOutputReadLine();
p、 WaitForExit();
}
此方法将文件夹压缩为7-Zip文件。您可以使用onProgress参数(在另一个线程中调用)来处理状态-它将包含状态的百分比。控制台按其应该的方式隐藏,但p.StandardOutput.ReadLine()不可用;没有返回压缩进度,因此我可以实时显示7za.exe的状态process@Bhargav为什么要删除答案的这一部分?这有NAA标志吗?是的,@Stijn删除了最后一部分,因为剩下的部分是有效的答案。