2

inside my C# app I runs a 7z process to extract an archive into it's directory

the archive is located in a random-named directory on the %TEMP% directory for example

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

(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 ?

Hanan
  • 1,395
  • 4
  • 18
  • 29

3 Answers3

5

You're using 7 Zip as an external process here. 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.

Sharp Zip Lib is fairly well reknowned but heres a specific wrapper library for using the 7zip archive

Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
  • however I chose not to go that way because at least the standard 7z library offers zipping at the file level only w/o archiving multiple files into one – Hanan Dec 18 '09 at 13:33
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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • The way I got error message that pointed me to the problem was simply running the same thing using command-line (cmd). – Hanan Nov 03 '08 at 13:55
0

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). Solutions to this can be found here standard way to convert to short path in .net

Community
  • 1
  • 1
Hanan
  • 1,395
  • 4
  • 18
  • 29