P4命令行通过c#

时间:2016-01-13 08:47:16

标签: c# perforce

有一种方法可以通过c#代码使用p4命令行吗? 我知道p4 API有一种方法,但由于业务原因,我只需要使用p4命令行而不是p4 API。 我试过的代码效果不好,代码如下:

public void runP4process()
    {
        try
        {              
            System.Environment.SetEnvironmentVariable("P4PORT", "10.31.0.225:1666");
            System.Environment.SetEnvironmentVariable("P4USER", "***");
            System.Environment.SetEnvironmentVariable("P4CLIENT", "**_client");
            System.Environment.SetEnvironmentVariable("P4PASSWD", "123456");

            Process P4Process = new Process();
            ProcessStartInfo P4ProcessStartInfo = new ProcessStartInfo();
            P4ProcessStartInfo.FileName = @"C:\Program Files\Perforce\p4.exe";
            P4ProcessStartInfo.Arguments = "p4 client";
            P4ProcessStartInfo.RedirectStandardOutput = true;
            P4ProcessStartInfo.RedirectStandardError = true;
            P4ProcessStartInfo.RedirectStandardInput = true;

            P4ProcessStartInfo.CreateNoWindow = true;
            P4ProcessStartInfo.UseShellExecute = false;
            P4ProcessStartInfo.WorkingDirectory = @"C:\Users\***\Documents\visual studio 2013\Projects\P4TestCmd";

            StringBuilder output = new StringBuilder();
            StringBuilder error = new StringBuilder();

            AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
            AutoResetEvent errorWaitHandle = new AutoResetEvent(false);

            readProcessIO(P4Process, output, error, outputWaitHandle, errorWaitHandle);

            P4Process.StartInfo = P4ProcessStartInfo;
            P4Process.Start();

            P4Process.BeginOutputReadLine();
            P4Process.BeginErrorReadLine();               
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message); ;
        }
    }

    private static void readProcessIO(Process P4Process, StringBuilder output, StringBuilder error, AutoResetEvent outputWaitHandle, AutoResetEvent errorWaitHandle)
    {
        P4Process.OutputDataReceived += (sender, e) =>
        {
            if (e.Data == null)
            {
                try
                {
                    outputWaitHandle.Set();
                }
                catch { }
            }
            else
            {
                output.AppendLine(e.Data);
            }
        };

        P4Process.ErrorDataReceived += (sender, e) =>
        {
            if (e.Data == null)
            {
                try
                {
                    errorWaitHandle.Set();
                }
                catch
                {
                }
            }
            else
            {
                error.AppendLine(e.Data);
            }
        };
    }
}

我得到的错误消息是:未知命令。尝试'p4 help'获取信息

这里有什么问题? TNX!

0 个答案:

没有答案