2

在我的 C# 应用程序中,我运行一个 7z 进程以将存档提取到它的目录中

例如,存档位于 %TEMP% 目录上的随机命名目录中

C:\Documents and Settings\User\Local Settings\Temp\vtugoyrc.fd2

(fullPathFilename = "C:\Documents and Settings\User\Local Settings\Temp\vtugoyrc.fd2\xxx.7z")

我的代码是:

sevenZipProcessInfo.FileName = SEVEN_ZIP_EXECUTABLE_PATH;
sevenZipProcessInfo.Arguments = "x " + fullPathFilename;
sevenZipProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;
sevenZipProcessInfo.UseShellExecute = true;
sevenZipProcessInfo.WorkingDirectory = Path.GetDirectoryName(fullPathFilename);
Process sevenZipProcess = Process.Start(sevenZipProcessInfo);
if (sevenZipProcess != null)
{
    sevenZipProcess.WaitForExit();
    if (sevenZipProcess.ExitCode != 0)
         ...exit code is 2 (fatal error by the 7z help)

我在哪里可以找到更详细的文档?

4

3 回答 3

5

您在这里使用 7 Zip 作为外部进程。它相当于直接从命令行调用命令。

您是否考虑过使用实际的库来压缩/解压缩文件。您可以在 C# 项目中引用的内容。

Sharp Zip Lib相当有名,但这里有一个特定的包装库,用于使用 7zip 存档

于 2008-11-03T09:28:13.150 回答
2

Assuming that the process writes errors to stderr/stdout, you could set UseShellExecute to false, and redirect stdout/stderr; there is an example on MSDN here (stderr) and here (stdout).

If you need to read from both stderr and stdout, and use WaitForExit(), then things get more interesting - usually involving either a few threads, or async methods.

One other final option is to use pipe redirection in the command - i.e. 1>out.txt 2>&1 - this pipes stdout into out.txt, and pipes stderr into stdout, so this also goes into out.txt. Then read from out.txt.

于 2008-11-03T11:02:06.720 回答
0

感谢所有帮助。

无论如何,问题是使用 'long-path-name' -> 命令行进程找不到 C:\Documents and Settings\ (因为名称中的空格)。可以在此处找到解决方案在.net 中转换为短路径的标准方法

于 2008-11-03T13:52:38.890 回答