1

I'm trying to run some Git commands from a C# program using System.Diagnostics.Process and it's not working. Git is in my path and when I try to run the command at the command prompt it works fine. I've tried capturing the standard output using a nested using() statement and that's not helping. When it gets to the reader.ReadToEnd() it just shows the DOS window hung with nothing in it. Here's my code:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
startInfo.WorkingDirectory = "c:\\gitmover";
startInfo.FileName = "cmd.exe";
startInfo.Arguments = " git add \"*.*\"";
using (Process process = Process.Start(startInfo))
{
      using (StreamReader reader = process3.StandardOutput)
      {
          string result = reader.ReadToEnd();
      }
}

Any ideas what I could be doing wrong?

Ben_G
  • 770
  • 2
  • 8
  • 30
  • Is the git exe on your path? The windows cmd.exe and the git bash don't always share path. For example, I got bit by the differences between the windows / mingw 'echo' command the other day --> http://superuser.com/questions/880429/why-does-cygwin-sha512-create-different-checksums-depending-on-terminal – Gojira Feb 26 '15 at 19:02
  • Yes - the git.exe is definitely in my path. Like I said, when I run the command in a command prompt it runs fine. – Ben_G Feb 26 '15 at 19:03
  • Is `git` in your `c:\gitmover` directory? – Yuval Itzchakov Feb 26 '15 at 19:04
  • It sounds like you might be experiencing this issue: http://stackoverflow.com/questions/7160187/standardoutput-readtoend-hangs – Erik Feb 26 '15 at 19:12
  • I took a look at that article and there doesn't seem to be a solution for me there. I tried `while (process.StandardOutput.Peek() > -1)` and it still hangs after it reads three lines. – Ben_G Feb 26 '15 at 20:14

0 Answers0