Git从批处理文件中提取不同的用户

时间:2012-11-21 14:33:49

标签: c# asp.net git batch-file

我有一个用户,我们称之为“MyUser”。它有一个密码,假设它是“密码”。该用户有一个用于git的SSH密钥。我尝试从我的ASP.NET应用程序运行一个发出git命令的批处理文件,它位于一个作为参数传递的位置。我的功能如下:

    private void ExecuteCommand(string path, int timeout)
    {
        Process process = new Process();

        process.StartInfo = new ProcessStartInfo();
        process.StartInfo.FileName = "cmd.exe";
        process.StartInfo.Arguments = "\"" + path + "\"";
        process.StartInfo.CreateNoWindow = true;
        process.StartInfo.UseShellExecute = false;
        //processInfo.WorkingDirectory = Config.GitHubRepoPath;
        process.StartInfo.UserName = "MyUser";
        process.StartInfo.Password = new System.Security.SecureString();
        process.StartInfo.Password.AppendChar('P');
        process.StartInfo.Password.AppendChar('a');
        process.StartInfo.Password.AppendChar('s');
        process.StartInfo.Password.AppendChar('s');
        process.StartInfo.Password.AppendChar('w');
        process.StartInfo.Password.AppendChar('o');
        process.StartInfo.Password.AppendChar('r');
        process.StartInfo.Password.AppendChar('d');
        // *** Redirect the output ***
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;

        process.Start();

        // *** Read the streams ***
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();

        if (timeout <= 0)
        {
            process.WaitForExit();
        }
        else
        {
            process.WaitForExit(timeout);
        }


        int exitCode = process.ExitCode;
        process.Close();
        return new ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output };
    }

但是当我运行此函数时,ExitCode为-1073741502,错误和输出为空。我该如何解决这个问题?

请帮助我,我已经尝试了几天解决这个问题。

1 个答案:

答案 0 :(得分:0)

我认为重定向标准错误和标准输出&amp;试图同步消费是错误的。请看这个链接: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput%28v=vs.100%29.aspx

请允许我复制摘录:

如果父进程调用p.StandardOutput.ReadToEnd后跟p.StandardError.ReadToEnd并且子进程写入足够的文本来填充其错误流,则会导致死锁条件。父进程将无限期地等待子进程关闭其StandardOutput流。子进程将无限期地等待父进程从完整的StandardError流中读取。

另一件事是......当您调用cmd.exe实例时,请尝试添加“/ c”参数。