proc.StandardOutput.ReadLine() returns 空字符串,当我要从我的 c# 代码执行 运行 p4 (Perforce) 命令时

proc.StandardOutput.ReadLine() returns empty string when I am going to run a p4 (Perforce) command from my c# code

我在 C# 中有一个工作代码(当然在另一台机器上),在迁移之后,它不再存在了。该代码在命令提示符下向 perforce 服务器运行命令,并读取输出以查找特定字符串。代码是:

string result="";
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;

            //Start the process
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
            //Attach output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;
            System.IO.StreamWriter sIn = proc.StandardInput;

            sIn.WriteLine(@"p4 changelists -m 1 -s submitted //<SOME_PATH_HERE>");
            proc.WaitForExit(500);
            sIn.WriteLine("EXIT");

            while((result=sOut.ReadLine()) != null)
            {
                if(result != "")
                {
                    if(result.Substring(0, 6) == "Change")
                    {
                        //get Changelist number
                        result = result.Substring(7, 6);
                        break;
                    }
                }
            }
            if((result == "") || (result == null))  //if perforce goes down
            {

问题是当我发出一些 cmd.exe 众所周知的命令时,例如 DIR 和 ... 我可以在我的结果变量中逐行看到输出但是对于 p4 的这个特殊命令,结果字符串是空。

我用谷歌搜索了我的问题,最接近的可能与 CASPOL 有关!?

我完全不知道它是什么以及如何摆脱它。

如有任何帮助,我们将不胜感激。

我对 C# 不够熟悉,无法调试您代码的 RedirectStandardError 部分,但是 通常 如果您正在编写 p4 命令脚本并且它似乎无声地失败了,这意味着您没有捕获 stderr。

我可以看到您的代码明确捕获标准输出;它可能需要做这样的事情吗?

System.IO.StreamReader sErr = proc.StandardError;
...
while((result=sErr.ReadLine()) != null)
   ...

我注意到您的代码没有提供任何连接信息,所以我猜测它在迁移后失败的原因是它依赖于旧机器环境中的 authentication/connection 设置(例如有效值P4USERP4PORT 以及带有有效身份验证票证的 P4TICKETS 文件)。如果您能够从失败的命令中获取 stderr,它将为您提供更多信息(例如 "can't connect to server" 或 "authentication failed" 或 "user unknown")。