Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C#运行命令和捕获标准输出时出现问题_C#_Pvcs - Fatal编程技术网

从C#运行命令和捕获标准输出时出现问题

从C#运行命令和捕获标准输出时出现问题,c#,pvcs,C#,Pvcs,我正试图从C#运行命令行实用程序PCLI.exe,但运气不好。我正在构造ProcessStartInfo对象,并已将process.StartInfo.RedirectStandardOutput=true设置为true,但当我尝试读取process.StandardOutput时,出现以下错误: Message=StandardOut尚未重定向或进程尚未启动。 我甚至尝试将命令的输出管道化到output.txt,当文件被创建时,它是空的 这个过程完成了,但并没有真正执行预期的文件,所以我试图捕

我正试图从C#运行命令行实用程序PCLI.exe,但运气不好。我正在构造ProcessStartInfo对象,并已将process.StartInfo.RedirectStandardOutput=true设置为true,但当我尝试读取process.StandardOutput时,出现以下错误:

Message=StandardOut尚未重定向或进程尚未启动。

我甚至尝试将命令的输出管道化到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();
    }
System.Diagnostics.Process进程=新的System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo=新
System.Diagnostics.ProcessStartInfo();
process.StartInfo.RedirectStandardOutput=true;
startInfo.UseShellExecute=false;
startInfo.WindowStyle=System.Diagnostics.ProcessWindowStyle.Normal;
startInfo.WorkingDirectory=“c:\\gitmover”;
startInfo.FileName=“C:\\ProgramFiles(x86)\\Serena\\vm\\win32\\bin\\pcli.exe”;
Console.WriteLine(“密码:?”);
字符串密码=Console.ReadLine();
对于(int i=0;ioutput.txt”;
process.StartInfo=StartInfo;
process.Start();
string strOutput=process.StandardOutput.ReadToEnd();
//等待进程完成
process.WaitForExit();
}

尝试使用将流程包装在
中,并使用
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();
                    }                  
                }

我写了下面的代码,对我来说效果很好。请注意,只有退出代码为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);

尝试将您的流程放入一个文件中,并使用流读取器读取std。接听来电!输入上面的代码会出现编译错误:当前上下文中不存在名称“start”。另外,如果我在using语句中启动进程,如何将System.Diagnostics.ProcessStartInfo中的值分配给该进程?很抱歉,上面的代码只是一个示例,不是复制粘贴解决方案。Process.Start接受您的Start选项,这就是上面代码中的Start。@Ben\G我已经更新了我的答案,向您显示ProcessStartInfo。如果这能满足您的需要,请让我知道,如果是这样,请接受答案。@Ben_G这样做最终解决了您的问题。将答案标记为已接受也有助于其他人解决同样的问题。@DavidWatts:我发现如果PVCS退出代码不是0,则所有结果都将重定向到StandardError。问题解决了。