Resolved "git stash push" failed to stash multiple files in C#

Joined
Nov 5, 2020
Messages
9
Programming Experience
10+
I've clone the repo and made changes to few files in the repo
tried to stash them and pls. find the below code.
The program doesn't actually seem to end and still running even after 15 minutes of run.
C#:
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.FileName = "cmd";
string repoFolder = @"cd ""E:\sunflower"""; // Navigate to repo folder
proc.StartInfo.Arguments = " git stash push";  
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd().Trim();
string errors = proc.StandardError.ReadToEnd().Trim();
 
Last edited by a moderator:
In the future, please put your code in code tags.
 
How exactly are you navigating to a folder with your line #4? Or that line of code not relevant here?
 
Pls. use these line instead of above and need help to resolve. its hanging with no result even after 15 mins. at proc.WaitForExit();
Worked on Windows 10 with Visual studio 2019

Steps: Do these manual steps first from command prompt
  1. Clone the repo (git clone reponame)
  2. navigate to repo folder and make some dummy changes to the files
  3. Use below code and try to make git stash push

Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.FileName = "cmd";

string drive = @"cd E:\Gaming Accessibility\sunflower"; // git cloned repo exist in E:\Gaming Accessibility\sunflower folder. Navigate to this repo folder and // execute git stash push

proc.StartInfo.Arguments = drive + " & git stash push";

proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd().Trim();
string errors = proc.StandardError.ReadToEnd().Trim();
 
Last edited:
In the future, please put your code in code tags.
I'm not in the habit of fixing your posts because you are to lazy or inept to follow a simple request. Either use code tags when posting to the forum, or I will close your topic without warning next time.
 
Thank you!! Placed as suggested. Would you please help on it.

C#:
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.FileName = "cmd";
string drive = @"cd E:\Gaming Accessibility\sunflower"; // git cloned repo exist in E:\Gaming Accessibility\sunflower folder. Navigate to this repo folder and // execute git stash push
proc.StartInfo.Arguments = drive + " & git stash push";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit();
string output = proc.StandardOutput.ReadToEnd().Trim();
string errors = proc.StandardError.ReadToEnd().Trim();
 
Last edited by a moderator:
I volunteer at this site to destress and get a break from work. I deal with user tickets being assigned to me as my day job. I'll respond when I have time and so inclined to do so.
 
Have resolved the issue long ago and here is the resolution.

C#:
                    var outputSb = new StringBuilder();
                    var errorSb = new StringBuilder();

                    proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    proc.StartInfo.RedirectStandardOutput = true;
                    proc.StartInfo.RedirectStandardError = true;
                    proc.StartInfo.UseShellExecute = false;
                    proc.OutputDataReceived += ((sender, stdout) =>
                    {
                        outputSb.Append(stdout.Data);
                    });
                    proc.ErrorDataReceived += ((sender, stdout) =>
                    {
                        errorSb.Append(stdout.Data);
                    });

                    proc.Start();

                    proc.BeginOutputReadLine();
                    proc.BeginErrorReadLine();

                    proc.WaitForExit();

                    output = outputSb.ToString();
                    error = errorSb.ToString();
 
Glad you figured it out.
 
Back
Top Bottom