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

问题描述 投票:0回答:1

我在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的特殊命令,结果字符串为空。

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

我不知道什么是流氓行为,如何摆脱它。

任何帮助将不胜感激。

c# asp.net stdout stdin perforce
1个回答
0
投票

我对C#不够熟悉,无法调试代码的RedirectStandardError部分,但是通常如果您正在编写p4命令的脚本,并且它似乎在无提示地失败,则表明您没有捕获标准错误。

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.