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

我在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有关!

我不知道这是什么精妙之处,以及如何摆脱它。

任何帮助将不胜感激。

LOVE11203 回答:当我要从我的C#代码运行p4(Perforce)命令时,proc.StandardOutput.ReadLine()返回空字符串

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

我可以看到您的代码明确捕获了stdout;也许需要做这样的事情吗?

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

我注意到您的代码未提供任何连接信息,因此我猜测迁移后为何失败的原因是它取决于旧计算机环境中的身份验证/连接设置(例如{{1}的有效值) }和P4USER以及带有有效身份验证票证的P4PORT文件)。如果您能够从失败的命令中获取stderr,它将为您提供更多信息(例如“无法连接到服务器”或“身份验证失败”或“用户未知”)。

本文链接:https://www.f2er.com/3136632.html

大家都在问