3 回答

TA貢獻1829條經驗 獲得超7個贊
我知道這是個老問題,幾天前我還在添加答案,我也面臨同樣的問題。
我認為你缺少的是-c參數。我使用了下面的代碼,它解決了這個問題。-c告訴 git-bash 執行后面的任何內容,它類似于-cmd命令行中的參數。
在下面提到的功能中 -
fileName= git-bash.exe 的路徑。
command= 你要執行的 git 命令。
workingDir= git 存儲庫的本地路徑。
public static void ExecuteGitBashCommand(string fileName, string command, string workingDir)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName, "-c \" " + command + " \"")
{
WorkingDirectory = workingDir,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = Process.Start(processStartInfo);
process.WaitForExit();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
var exitCode = process.ExitCode;
process.Close();
}
我希望它能解決問題。

TA貢獻1783條經驗 獲得超4個贊
我認為你是在正確的道路上。我會嘗試在路徑中使用 git,但也應該可以git-bash.exe直接使用,在我的機器上,它位于此處:C:\Program Files\Git\git-bash.exe。
Process gitProcess = new Process();
gitInfo.Arguments = YOUR_GIT_COMMAND; // such as "fetch origin"
gitInfo.WorkingDirectory = YOUR_GIT_REPOSITORY_PATH;
gitInfo.UseShellExecute = false;
gitProcess.StartInfo = gitInfo;
gitProcess.Start();
string stderr_str = gitProcess.StandardError.ReadToEnd(); // pick up STDERR
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT
gitProcess.WaitForExit();
gitProcess.Close();

TA貢獻1828條經驗 獲得超3個贊
就像@S.Spieker 已經在它的好答案中告訴你的那樣,不需要使用 git bash (它更難實現并且性能更低),只需直接調用 git 可執行文件即可。
您可以查看執行此操作的 GitExtensions 代碼:https ://github.com/gitextensions/gitextensions/blob/027eabec3be497f8d780cc68ece268c64a43a7d5/GitExtensionsVSIX/Git/GitCommands.cs#L112
您還可以使用 libgit2sharp ( https://github.com/libgit2/libgit2sharp )實現您想要的。如果您想解釋命令運行的結果,那可能會更容易。
- 3 回答
- 0 關注
- 267 瀏覽
添加回答
舉報