tags:

views:

844

answers:

3

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 ?

+4  A: 

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
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
+1  A: 

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
The way I got error message that pointed me to the problem was simply running the same thing using command-line (cmd).
Hanan
A: 

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 http://stackoverflow.com/questions/258367/standard-way-to-convert-to-long-path-in-net

Hanan