Reputation: 1445
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 ?
Upvotes: 2
Views: 3918
Reputation: 1445
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
Upvotes: 0
Reputation: 1063005
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
.
Upvotes: 2
Reputation: 44278
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
Upvotes: 5