我们有一个.NET应用程序,可以使用winzip (winzip32.exe)自动压缩文件,因为.The进程类似于从命令行使用winzip压缩文件。
我们已经配置了一个调度程序来每天运行这个应用程序,并且它已经成功地运行了很长时间。
几天前,我们遇到了一个问题,没有创建zip文件。但是,我看到winzip实例已经创建并正在运行,但是没有发生压缩。在发行当天,机器上没有观察到不平衡的情况。
请您帮助我们什么可能是问题,或在什么情况下,进程未能压缩文件。
供参考的代码片段:
string WinzipPath = ConfigurationManager.AppSettings["WinzipPath"] ;
System.Diagnostics.Process objProc = new System.Diagnostics.Process();
objProc.StartInfo.FileName = WinzipPath;
if(strPassword != "")
{
objProc.StartInfo.Arguments = string.Format("-min -a -en -r -s\"{0}\" {1} {2}", strPassword, strzipFilePath, strFileNames);
}
else
{
objProc.StartInfo.Arguments = string.Format("-min -a -en -r \"{0}\" {1}", strzipFilePath, strFileNames);
}
objProc.StartInfo.RedirectStandardOutput = true;
objProc.StartInfo.UseShellExecute = false;
objProc.StartInfo.CreateNoWindow = true;
objProc.Start();
objProc.WaitForExit();提前感谢
发布于 2011-08-11 11:52:18
我同意您的评论,在应用程序中使用DotNetZip更好。
即使你不接受这个建议,如果你继续使用wzzip.exe,你也可以做一些简单的事情来改善你的生活。首先:收集并记录标准输出和标准错误。您的代码重定向标准输出,但不记录或显示它。它忽略了标准错误消息。第二:检查流程的退出代码。
根据我的经验,当命令行winzip程序失败时,它会向它的stdout发出一些信息,这描述了失败。“未找到文件”或“不一致的选项”或类似的内容。
另外: wzzip.exe的输出非常冗长。它在运行时发出进度消息,然后发出后台空间以“擦除”最近的消息,然后发出另一条进度消息,等等。80%或更多的wzzip.exe输出可以是后置空间。有了所有这些输出,wzzip.exe可以填充输出缓冲区。如果您没有在应用程序中阅读它们,您可能会陷入僵局。
因此,您应该阅读stdout和stderr,原因有两个:检查结果,并避免由完全输出缓冲区造成的死锁。
dotnetzip的测试套件包括成功运行wzzip.exe的代码,以确保winzip与DotNetZip兼容,反之亦然。这基本上就是它看起来的样子(取自TestUtilities.cs):
public void Exec(string program, string args)
{
System.Diagnostics.Process p = new System.Diagnostics.Process
{
StartInfo =
{
FileName = program, // wzzip.exe in your case
CreateNoWindow = true,
Arguments = args, // whatever you like
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
UseShellExecute = false,
}
};
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
// Must read at least one of the stderr or stdout asynchronously,
// to avoid deadlock. I choose to read stderr asynchronously.
var sb = new StringBuilder();
p.ErrorDataReceived += new DataReceivedEventHandler((o, e) => {
if (!String.IsNullOrEmpty(e.Data))
sb.Append(e.Data);
});
p.Start();
p.BeginErrorReadLine();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// Important:
// Display or log the output here. stdout output is available in variable "output";
// stderr is available in sb.ToString()
if (p.ExitCode != 0)
throw new Exception(String.Format("Non-zero return code {0}",
p.ExitCode));
}此代码将与任何发出输出的exe一起工作。
还有一种方法,没有在这里显示,它从wzzip.exe输出中剥离了后置空间。当将输出以字符串的形式显示给人时,可以更容易地理解输出,比如在MessageBox或其他方面。检查该方法的TestUtilities代码。
ps:只要重读你的Q,我看到你提到了winzip32.exe。我不知道winzip32.exe是什么。winzip发布的工具的命令行版本是wzzip.exe。我关于输出和后置空间的评论适用于该工具。
https://stackoverflow.com/questions/7014142
复制相似问题