Trouble running command from C# and capturing StandardOutput

748 views Asked by At

I'm trying to run the command line utility PCLI.exe from C# and having no luck. I'm constructing a ProcessStartInfo object and have set process.StartInfo.RedirectStandardOutput = true, but when I try to read process.StandardOutput I get the following error:

Message=StandardOut has not been redirected or the process hasn't started yet.

I even tried just piping the output of my command to output.txt, and while the file gets created it's empty.

The process completes but doesn't really execute the intended file, so I'm trying to capture StandardOutput to see what's going on. Just for background purposes I'm trying to run a PVCS get command to get a file out of PVCS.

Here's a snippet of my code:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new     
System.Diagnostics.ProcessStartInfo();
process.StartInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory = "c:\\gitmover";
startInfo.FileName = "C:\\Program Files (x86)\\Serena\\vm\\win32\\bin\\pcli.exe";

Console.WriteLine("Password:?");
string password = Console.ReadLine();
for (int i = 0; i < revisionsArray.Count(); i++)
    {
       string fileName = "/" + file.Key.Substring(file.Key.IndexOf("Customers")).Replace('\\','/');
       startInfo.Arguments = "get -r" + revisionsArray[i] + " -id\"beng:" + password + "\" -prM:\\Engineering\\SOUP -o -ac:/gitmover -bp'/Customers' -z " + fileName + "> output.txt";
       process.StartInfo = startInfo;
       process.Start();
       string strOutput = process.StandardOutput.ReadToEnd();

       //Wait for process to finish
       process.WaitForExit();
    }
2

There are 2 answers

12
David Watts On

Try wrapping your process in a using and use a StreamReader to read stdout.

var start = new ProcessStartInfo
            {
                FileName = _pathToPythonExecutable,
                Arguments = string.Format(" {0}", _pathToPythonCalibratorScript),
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                RedirectStandardError = true,
                WorkingDirectory = _currentWorkingDirectory            
            };

using (Process process = Process.Start(start))
                {                  
                    using (StreamReader reader = process.StandardOutput)
                    {
                        result = reader.ReadToEnd();
                    }                  
                }
0
Amir On

I wrote below code and it works fine for me. Please take note that only those commands with exit code 0 will appear in StandardOutput otherwise you need to check StandardError.

public class ProcessStarter
    {
        public static OutputEventArgs execAsync(string exe, string arguments)
        {
            OutputEventArgs oea = new OutputEventArgs();
            try
            {
                using (Process myProcess = new Process())
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardError = true;
                    startInfo.UseShellExecute = false;
                    startInfo.CreateNoWindow = true;

                    startInfo.FileName = exe;
                    startInfo.Arguments = arguments;
                    myProcess.StartInfo = startInfo;
                    myProcess.Start();
                    oea.Data = myProcess.StandardOutput.ReadToEnd();
                    oea.ErrorMsg = myProcess.StandardError.ReadToEnd();
                    myProcess.WaitForExit();
                    oea.exitCode = myProcess.ExitCode;
                }
            }catch(Exception e)
            {
                oea.Data = e.Message;
                oea.ExceptionHappened();
            }
            return oea;
        }

    }

    public class OutputEventArgs
    {
        public int exitCode { get; set; }
        public OutputEventArgs() { Error = false; }
        public string Data { get; set; }
        public bool Error { get; set; }
        public bool ErrorMsg { get; set; }


        public void ExceptionHappened()
        {
            exitCode = int.MinValue;
            Error = true;
            Data = string.Empty;
        }

    }

How to use it?

 string arguments = "get -pr" + tbProjectDatabase.Text.Trim() +
                                           " -id" + tbUsername.Text.Trim() + ":" + tbPassword.Text.Trim() +
                                           " -a'" + pvcsFolder + "' -o -z '" + tbCurrentProjectLocation.Text.Trim() + zipItem.FullNameZip + "'";

                    oea = ProcessStarter.execAsync("pcli", arguments);