无法从C#运行命令并捕获StandardOutput

时间:2015-02-26 17:05:46

标签: c# pvcs

我正在尝试从C#运行命令行实用程序PCLI.exe并且没有运气。我正在构建一个ProcessStartInfo对象,并设置了process.StartInfo.RedirectStandardOutput = true,但是当我尝试读取process.StandardOutput时,我收到以下错误:

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

我甚至尝试将命令的输出传递给output.txt,并且在创建文件时它是空的。

该过程完成但并未真正执行目标文件,因此我尝试捕获StandardOutput以查看正在发生的事情。仅出于背景目的,我正在尝试运行PVCS get命令以从PVCS中获取文件。

以下是我的代码片段:

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 个答案:

答案 0 :(得分:1)

尝试将您的流程打包到using并使用StreamReader来读取标准输出。

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

答案 1 :(得分:0)

我写了下面的代码,对我来说很好用。请注意,只有那些退出代码为0的命令才会出现在StandardOutput中,否则您需要检查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;
        }

    }

如何使用它?

 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);
相关问题