1

我正在使用 P4Api 使用 C# 构建一个应用程序,但我需要使用参数 -zprog 和 -zversion 以便服务器日志显示命令来自哪个应用程序,正如这篇文章所解释的那样:https ://community.perforce.com/ s/article/11551

string logParams = "-zprog=MyApp -zversion=1.1";
P4Command cmd = new P4Command(rep, "sync", false, path);
  • 我试图在路径之前和之后将 logParams 作为参数传递给 P4Command,但它会将它们识别为文件的另一个路径,返回“没有这样的文件”消息错误。
  • 我尝试在“同步”之前添加它,但它会将其识别为命令,因此它返回“未知命令”消息错误。

如链接所述,使用 cmd,此命令应为“p4 -zprog=MyApp -zversion=1.1 sync [path]”,因此此参数应用于“p4”而不是“sync”

是否可以将此参数添加到命令中?如果没有,关于如何做到这一点的任何建议?

谢谢。

4

1 回答 1

1

正如评论中所建议的,我最终摆脱了 P4Api,基本上我使用 aSystem.Diagnostics.Process来调用 p4.exe 并传递一些参数来做我需要做的任何事情。这是我的解决方案:

public bool RunCommand<T>(string command, Func<string, string, T> output,out T outputResult, params string[] args)
{
    string logParams = "-zprog=MyApp -zversion=1.1";
    Process proc = new Process();
    proc.StartInfo.WorkingDirectory = "";
    proc.StartInfo.FileName = "p4.exe";
    proc.StartInfo.Arguments = logParams + " " + command + " ";
    foreach (string s in args)
    {
        proc.StartInfo.Arguments += s + " ";
    }

    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.CreateNoWindow = true;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.Start();
    StreamReader strOutput = proc.StandardOutput;
    StreamReader strError = proc.StandardError;
    outputResult = output(strOutput.ReadToEnd(), strError.ReadToEnd());
    proc.WaitForExit();
    return true;
}

Func<string, string, T> output允许我解析进程输出和错误,从 P4 查询中获取所需的数据并将其返回out T outputResult,例如从工作区路径返回 Depot 路径的命令“where”。

我希望这对其他有类似问题的人有所帮助。

于 2020-01-30T12:14:11.250 回答