C# Process.Start Multiple Commands

S

stemp1ar

I am wondering if it possible to open a single process and run
multiple commands on that process and check standard error and
standard out after each command? Has anyone done something similar
and has an example?

I am expecting it would look something like below...

// Execute the first command
proc.StartInfo.FileName = @"cmd.exe";
proc.StartInfo.Arguments = @"dir";
proc.Start();
Console.WriteLine("Error Output: " + proc.StandardError.ReadToEnd());
Console.WriteLine("Standard Output: " +
proc.StandardOutput.ReadToEnd());

// Execute a second command
proc.StartInfo.FileName = @"cmd.exe";
proc.StartInfo.Arguments = @"cd foo";
proc.Start();
Console.WriteLine("Error Output: " + proc.StandardError.ReadToEnd());
Console.WriteLine("Standard Output: " +
proc.StandardOutput.ReadToEnd());

// Execute the third command
proc.StartInfo.FileName = @"cmd.exe";
proc.StartInfo.Arguments = @"dir";
proc.Start();
Console.WriteLine("Error Output: " + proc.StandardError.ReadToEnd());
Console.WriteLine("Standard Output: " +
proc.StandardOutput.ReadToEnd());

proc.WaitForExit();
Console.WriteLine("Exit Code: " + proc.ExitCode);
 
G

Guest

I believe you will need to create a batch file, then run the batch file with
the process object that you create. Separate calls to process start will not
have a memory of the previous calls to the start method. Your change of
directory won't have an effect unless you change the start directory on the
process befor the last call to start.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top