C# 从作为进程运行的控制台读取文本(输出?)

C# 从作为进程运行的控制台读取文本(输出?),c#,C#,我试图检查git存储库的用户名和密码是否有效。在控制台中,我运行: git clone http://username:password@server/test.git 我得到: fatal: Authentication failed for ... 所以现在我知道用户名和密码是无效的。我正在尝试将此命令作为进程运行: var process = new Process { StartInfo = new ProcessStartInfo { Fil

我试图检查git存储库的用户名和密码是否有效。在控制台中,我运行:

git clone http://username:password@server/test.git
我得到:

fatal: Authentication failed for ...
所以现在我知道用户名和密码是无效的。我正在尝试将此命令作为进程运行:

var process = new Process
{
     StartInfo = new ProcessStartInfo
     {
          FileName = "git.exe",
          RedirectStandardInput = true,
          RedirectStandardOutput = true,
          RedirectStandardError = true,
          UseShellExecute = false,
          WorkingDirectory = "some_directory"
          CreateNoWindow = true,
           Arguments = "git clone http://username:password@server/test.git"
      },
};
process.Start();

我想访问此命令的结果。process.StandardErrorprocess.StandardOutput都等于string.Empty。有没有办法读取结果?

通常您应该读取结果

如果进程失败,返回值应为非
0
。当然,您只能在流程完成后检索退出代码

因此:

请注意:我还没有测试它,但它是标准惯例

要读取输出,通常使用:

string output = process.StandardOutput.ReadToEnd();
string err = process.StandardError.ReadToEnd();
Console.WriteLine(output);
Console.WriteLine(err);

您真正需要的是您的标准输出,可以通过以下方式访问:

string stdout = p.StandardOutput.ReadToEnd(); 
并使用
p.WaitForExit()之后,因为有时需要一段时间才能给出错误消息

string output = process.StandardOutput.ReadToEnd();
string err = process.StandardError.ReadToEnd();
Console.WriteLine(output);
Console.WriteLine(err);
string stdout = p.StandardOutput.ReadToEnd();