How do I execute 7za list command programmatically and output the results to a file?

323 views Asked by At

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?

1

There are 1 answers

2
Andrew Grinder On BEST ANSWER

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();