Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have implemented a small application which will invoke the Git-bash.exe and execute a command to get the list of files associated with the commit id. I am sure the whole code is correct except the way I pass the arguments to the Process. Can some one please let me know how to pass the Git bash argument to C# Process. I googled alot and dint find a right way to provide this argument.

I know the issue is in below line
gitInfo.Arguments = "-c \"" + "git diff-tree -r 61140bb40f879dd21e8624fe8e2125df846572e8" + "\"";

Can some one let me know how to pass the arguments in right way.

What I have tried:

ProcessStartInfo gitInfo = new ProcessStartInfo();
gitInfo.CreateNoWindow = false;
gitInfo.UseShellExecute = false;
gitInfo.RedirectStandardError = true;
gitInfo.RedirectStandardOutput = true;
gitInfo.FileName = @"C:\Program Files\Git\git-bash.exe";
Process gitProcess = new Process();

gitInfo.WorkingDirectory = @"D:\CodeBranch";
gitInfo.Arguments = "-c \"" + "git diff-tree -r 61140bb40f879dd21e8624fe8e2125df846572e8" + "\"";

gitProcess.StartInfo = gitInfo;

string stderr_str = gitProcess.StandardError.ReadToEnd();  // pick up STDERR
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT

Updated 23-Sep-20 19:40pm
Richard MacCutchan 22-Sep-20 11:25am    
That looks correct. What is the problem?
OriginalGriff 22-Sep-20 11:31am    
What it is doing that you didn't expect, or not doing that you did?
Are there any error messages?
What have you tried to find out what exactly is happening?
Sampath579 22-Sep-20 11:37am    
The git-bash window pop ups and disappear immediately. The same command git diff-tree -r 61140bb40f879dd21e8624fe8e2125df846572e8 if i enter manually in git-bash console then i get the list of files properly but when is send it as an arugment in c# process its failing to execute that command. If i comment that arguments part and execute my code then git-bash console is poping up and staying for longer time and hence i feel the way i pass the arguments to the process is wrong.
OriginalGriff 22-Sep-20 11:48am    
So what does the debugger say is in the strerr and strout streams?
What does it show - exactly what - is in the arguments property string? (I assume it's not a fixed ID value from the way you have the single argument string spaced out).
Sampath579 22-Sep-20 11:58am    
strerr_str is empty, since the git-bash console does not have any data in it and the window no longer exists during we read it. The ID is the commit id i have provided which is a valid one in my project.

When i check the value of arguments in visual studio during debugging the value of the arguments is as follows:
-c "git diff-tree -r 61140bb40f879dd21e8624fe8e2125df846572e8"

1 solution

I could able to solve this issue in different way. Since i am not aware of how to give this command line argument in c# Process, i written a batch file with all those GIT commands and called that batch file from c# code and it worked like a charm.. 
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900