-1

With origional testing in command prompt, I was able to output the results of a 7za list command, with findstr, to a text file and I needed this same behavior in my application.

Executing 7za.exe directly with other commands like findstr or a redirection operator >> resulted in exit code 7.

How can I execute a 7za command, programmatically (C#), to list contents based off of a findstr command and finally write those results to a text file?

Andrew Grinder
  • 585
  • 5
  • 21

1 Answers1

0

After some research and testing I was able to run cmd.exe and write commands to the process. I also did not have to call exit.

Process cmdProcess = new Process();
ProcessStartInfo processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = "cmd.exe";
processStartInfo.RedirectStandardInput = true;
processStartInfo.UseShellExecute = false;

cmdProcess.StartInfo = processStartInfo;
cmdProcess.Start();

String sevenZipListFindStrCommand = "Library\\7za.exe l \"C:\\temp\\testSource\\archive.7z\" -pXYZ | findstr /i VM >> C:\\temp\\output.txt";
using (StreamWriter streamWriter = cmdProcess.StandardInput)
{
    if (streamWriter.BaseStream.CanWrite)
    {
        streamWriter.WriteLine(sevenZipListFindStrCommand);
    }
}

cmdProcess.WaitForExit();
Andrew Grinder
  • 585
  • 5
  • 21
  • Just a notice: cmd won't run reliably if you are writing a windows service. By calling cmd, the system suppose that you are in the interactive mode. However, the same effect could be done by similar code: just calling "7za.exe" directly (processStartInfo.FileName = ".../7za.exe") & use StreamWriter to write the StandardOutput – Hoàng Long Oct 16 '15 at 09:42
  • @HoàngLong - Thanks for the advice! I'll have to check it out. – Andrew Grinder Oct 16 '15 at 13:44