【问题标题】:proc.StandardOutput.ReadLine() returns empty string when I am going to run a p4 (Perforce) command from my c# code当我要从我的 c# 代码运行 p4 (Perforce) 命令时,proc.StandardOutput.ReadLine() 返回空字符串
【发布时间】:2019-11-08 16:00:36
【问题描述】:

我在 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 和 ... p>

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: c# asp.net stdout stdin perforce


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 实际上我已经在代码的其他地方提供了连接凭据和其他东西。无论是否连接,当我逐行阅读输出时,Ib 都会期待一些错误消息。问题是即使对于连接错误消息,结果字符串也是空的
    • 正如我所说,如果您没有看到错误消息,通常意味着您没有查看 stderr。作为一种廉价的解决方法,您可以添加 -s 全局标志,它告诉 p4 使用标签将所有内容打印到标准输出,而不是将信息定向到标准输出并将错误定向到标准错误。
    • 是的,谢谢,我在 stderr 中收到错误,说它无法将 p4ticket 文件写入其路径。我授予它现在工作的路径的写权限。多谢大佬
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多