简体   繁体   English

如何从7z命令行过程中获取扩展的错误信息

[英]how to get extended error information from 7z commandLine process

inside my C# app I runs a 7z process to extract an archive into it's directory 在我的C#应用​​程序中,我运行7z进程以将存档提取到其目录中

the archive is located in a random-named directory on the %TEMP% directory for example 例如,归档文件位于%TEMP%目录中的随机命名目录中

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

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

my code is: 我的代码是:

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)

Where can I find more elaborate documentation ? 在哪里可以找到更详尽的文档?

You're using 7 Zip as an external process here. 您在这里使用7 Zip作为外部过程。 Its the equivalent of calling the commands directly from the command line. 这等效于直接从命令行调用命令。

Have you considered using an actual Library for zipping/unzipping your files. 您是否考虑过使用实际的库来压缩/解压缩文件。 Something you can reference in your C# project. 您可以在C#项目中引用的内容。

Sharp Zip Lib is fairly well reknowned but heres a specific wrapper library for using the 7zip archive Sharp Zip Lib众所周知,但是这里有一个使用7zip存档的特定包装库

Assuming that the process writes errors to stderr/stdout, you could set UseShellExecute to false, and redirect stdout/stderr; 假设该进程将错误写入stderr / stdout,则可以将UseShellExecute设置为false,然后重定向stdout / stderr; there is an example on MSDN here (stderr) and here (stdout). 有MSDN上的示例这里 (错误),并在这里 (标准输出)。

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. 如果您需要同时读取stderr stdout 使用WaitForExit() ,那么事情会变得更加有趣-通常涉及几个线程或异步方法。

One other final option is to use pipe redirection in the command - ie 1>out.txt 2>&1 - this pipes stdout into out.txt, and pipes stderr into stdout, so this also goes into out.txt. 另一种最终选择是在命令中使用管道重定向-即1>out.txt 2>&1这会将stdout管道传输到out.txt,并将stderr管道传输到stdout,因此这进入out.txt。 Then read from out.txt . 然后从out.txt读取。

Thanks for all help. 感谢所有帮助。

Anyhow the problem was the use of 'long-path-name' -> command-line process can't find C:\\Documents and Settings\\ (because of the spaces in the name). 无论如何,问题是使用“长路径名”->命令行过程找不到C:\\ Documents and Settings \\(由于名称中有空格)。 Solutions to this can be found here standard way to convert to short path in .net 解决方案可以在这里找到在.net中转换为短路径的标准方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM