Isuru
Isuru

Reputation: 3958

Run process using C#

I'm trying to run a program that converts unaligned zip file to aligned zip file. To do that Google provides zip align tool. It doesn't have GUI, so I'm trying to create C# WPF program.

I want to start that tool from my program, I tries various methods such as starting command prompt and run that tool using arguments. But it didn't work out.

And I used following method to start the process with arguments.

string genArgs = "f -4 C:\\Users\\Isuru\\OneDrive\\Freelancing\\DigitalClock\\app\\build\\outputs\\apk\\app-release-unaligned.apk outfile.apk";
string pathToFile = "C:\\Users\\Isuru\\AppData\\Local\\Android\\sdk\\build-tools\\23.0.2\\zipalign.exe";
Process runProg = new Process();
try
{
    runProg.StartInfo.FileName = pathToFile;
    runProg.StartInfo.Arguments = genArgs;
    runProg.StartInfo.CreateNoWindow = true;
    runProg.Start();
    // start our event pumps
    runProg.BeginOutputReadLine();
    runProg.BeginErrorReadLine();
    runProg.WaitForExit();

    Console.ReadLine();
}
catch (Exception ex)
{
    System.Console.WriteLine("Could not start program " + ex);
    Console.ReadLine();
}

But then I get following error. Is there any other method?

enter image description here

Upvotes: 0

Views: 144

Answers (1)

Dirk
Dirk

Reputation: 10958

Process.BeginOutputReadLine will throw an InvalidOperationException if RedirectStandardOutput is set to false which is the default value. If you want to capture the standard output then you have to redirct it, i.e. set RedirectStandardOutput to true:

runProg.StartInfo.RedirectStandardOutput = true;

The same applies to BeginErrorReadLine and RedirectStandardError.

runProg.StartInfo.RedirectStandardError = true;

Upvotes: 1

Related Questions