亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何通過c#代碼打開和使用Git Bash

如何通過c#代碼打開和使用Git Bash

C#
陪伴而非守候 2022-11-13 14:43:55
我正在嘗試包括打開 Git Bash,推入和拉入我的 c# 代碼。雖然打開 Git BashProcess.Start()不是問題,但我無法將命令寫入 Git Bash。我試過在 中包含命令ProcessStartInfo.Arguments,以及重定向標準輸出。兩者都沒有工作。在下方,您可以看到我嘗試過的不同代碼片段。private void Output(){    //Try 1    processStartInfo psi = new ProcessStartInfo();    psi.FileName = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk";    psi.UseShellExecute = false;    psi.RedirectStandardOutput = true;    psi.Argument = "git add *";    Process p = Process.Start(psi);    string strOutput = p.StandardOutput.ReadToEnd();    Console.WriteLine(strOutput);    //Try 2    ProcessStartInfo psi = new ProcessStartInfo(@"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk");    Process.Start(psi);    psi.Arguments = "git add *";    Process.Start(psi);    //Try 3    var escapedArgs = cmd.Replace("\"", "\\\"");    var process = new Process()    {        StartInfo = new ProcessStartInfo        {            FileName = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Git\Git Bash.lnk",            Arguments = "cd C:\\Users\\strit\\autocommittest2\\autocommittest2\n",             RedirectStandardOutput = true,             UseShellExecute = false,             CreateNoWindow = true,         }    };    process.Start();    string result = process.StandardOutput.ReadToEnd();    process.WaitForExit();}Git Bash 打開,但命令行中沒有寫入任何內容。
查看完整描述

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();

}

我希望它能解決問題。


查看完整回答
反對 回復 2022-11-13
?
慕娘9325324

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();


查看完整回答
反對 回復 2022-11-13
?
倚天杖

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 )實現您想要的。如果您想解釋命令運行的結果,那可能會更容易。


查看完整回答
反對 回復 2022-11-13
  • 3 回答
  • 0 關注
  • 267 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號