7z l my.zip> my.txt

This causes an error:

> log.txt

Unhandled exception of type "System.Exception" in file_backup.exe

More information: sevenzip.addtoarchive: Command line error

 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = archiver; startInfo.Arguments = " l "; startInfo.Arguments += "\"" + archiveName+ "\""; startInfo.Arguments += " > " + "log.txt"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; int sevenZipExitCode = 0; using (Process sevenZip = Process.Start(startInfo)) 
  • one
    I would venture to suggest that 7z knows nothing about the command line option ">". Try to run cmd.exe with the command to run 7Z - Vladimir Martyanov
  • one
    And also ProcessStartInfo has such a member as RedirectStandardOutput. - Akina
  • @ Vladimir Martianov the command is correct, BUT: 7z is not detected by default by the command line, so you need the full path to it or explicitly set it with the PATH environment variable; the names of the archive and the target file must also be with the full path and all necessary rights must be available. - rdorn
  • @ Vladimir Martiyanov you turned out to be partly right, for the console the command is correct, only there is not one but two commands, the second is for the console itself and you need to write it manually. The answer is added. - rdorn

1 answer 1

  1. Check that the path to 7z.exe registered in the PATH environment variable; without it, even from the command line you will get an error, or specify the path explicitly in the command.

  2. Check that the user on whose behalf the program is running has sufficient rights to read the archive and write the file to the appropriate folder.

  3. Specify the file paths explicitly and completely. I am not a fan of absolute paths, but it is worth starting with this, because relative paths sometimes lead to the wrong place. If the option with full paths works, you can begin to look for errors in relative paths.

  4. For the process, you can, and often need to, explicitly set the working directory, then problems with relative paths will be an order of magnitude less.


Found what the problem with your code, but it does not cancel all of the above.

The 7z l my.zip > my.txt for CMD or PowerShell is absolutely correct, but you can't just put it into the process launch arguments, since in fact, there are not one but two commands in this record: command for archiver 7z l my.zip — list files and command for console > my.txt — redirect output to a file.

Thus, we need to implement the same in the code, i.e. start the process process for the archiver, with its parameters and redirect the output to a file.

You can do it like this:

 static void Main() { ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = @"C:\Program Files\7-Zip\7z.exe", Arguments = @"l <путь к архиву>\my.zip", RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true }; using (Process sevenZip = new Process()) { sevenZip.StartInfo = startInfo; sevenZip.OutputDataReceived += proc_OutputDataReceived; sevenZip.Start(); sevenZip.BeginOutputReadLine(); sevenZip.WaitForExit(); } } private static void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) { using (var wr = File.AppendText(@"<путь к файлу>\log.txt")) wr.WriteLine(e.Data); }