Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过c#代码打开和使用Git Bash_C#_Windows_Git_Git Bash - Fatal编程技术网

如何通过c#代码打开和使用Git Bash

如何通过c#代码打开和使用Git Bash,c#,windows,git,git-bash,C#,Windows,Git,Git Bash,我试图包括打开GitBash,推拉我的c代码。虽然使用Process.Start()打开Git Bash不是问题,但我无法将命令写入Git Bash 我已经尝试在ProcessStartInfo.Arguments中包含命令,以及重定向标准输出。两者都没有起到任何作用。下面您可以看到我尝试过的不同代码片段 private void Output() { //Try 1 processStartInfo psi = new ProcessStartInfo(); psi.F

我试图包括打开GitBash,推拉我的c代码。虽然使用
Process.Start()
打开Git Bash不是问题,但我无法将命令写入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打开了,但命令行中没有写入任何内容。

我认为您的方法是正确的。我会尝试在路径中使用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();

我认为你走对了路。我会尝试在路径中使用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();

就像@S.Spieker在它的好答案中已经告诉过你的,不需要使用git bash(它使实现变得更困难,性能也更低),直接调用git可执行文件即可

您可以查看执行此操作的GitExtensions代码:


您还可以使用libgit2sharp()实现您想要的功能。如果您想解释命令运行的结果,这可能会更容易。

就像@S.Spieker在它的好答案中已经告诉您的那样,不需要使用git bash(它使实现更困难,性能更低),只需直接调用git可执行文件即可

您可以查看执行此操作的GitExtensions代码:


您还可以使用libgit2sharp()实现您想要的功能。如果您想解释命令运行的结果,这可能会更容易。

我知道这是一个老问题,仍然在添加答案,因为几天前我也面临同样的问题

我认为您缺少的是
-c
参数。我使用了下面的代码,它解决了这个问题
-c
告诉gitbash执行下面的任何操作,它类似于命令行中的
-cmd
参数

在下述职能中—

fileName
=git-bash.exe的路径

命令
=要执行的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();
}

我希望它能解决这个问题。

我知道这是一个老问题,仍然在添加答案,因为几天前我也面临着同样的问题

我认为您缺少的是
-c
参数。我使用了下面的代码,它解决了这个问题
-c
告诉gitbash执行下面的任何操作,它类似于命令行中的
-cmd
参数

在下述职能中—

fileName
=git-bash.exe的路径

命令
=要执行的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();
}

我希望它能解决这个问题。

谢谢,我更改了代码。问题:作为参数添加的命令没有作为常规Git Bash命令插入。如果命令只是“test”,那么我得到bash:/usr/bin/test:无法执行二进制文件Exit 126。如果命令是例如“cdc:”gitbash在打开后立即关闭。你知道为什么会这样吗?代码:ProcessStartInfo startInfo=newprocessstartinfo{FileName=@“C:\Program Files\git bash.exe”,Arguments=command,redirectStandardOutput=true,UseShellExecute=false,CreateNoWIndow=true,}Process pro=Process.Start(startInfo);支持WaitForExit();pro.Close();谢谢,我改了密码。问题:作为参数添加的命令没有作为常规Git Bash命令插入。如果命令只是“test”,那么我得到bash:/usr/bin/test:无法执行二进制文件Exit 126。如果命令是例如“cdc:”gitbash在打开后立即关闭。你知道为什么会这样吗?代码:ProcessStartInfo startInfo=newprocessstartinfo{FileName=@“C:\Program Files\git bash.exe”,Arguments=command,redirectStandardOutput=true,UseShellExecute=false,CreateNoWIndow=true,}Process pro=Process.Start(startInfo);支持WaitForExit();pro.Close();