c# - C# 函数执行批处理文件的 Git pull 问题

标签 c# git command-line

我有一个简单的批处理文件:

cd <path to my git repository>
git pull

我尝试使用以下 C# 函数来执行它:

private NuGetBO.ShellCommandReturn ExecuteCommand(string path)
{
    ProcessStartInfo processInfo;
    Process process;

    processInfo = new ProcessStartInfo(path);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;

    // *** Redirect the output ***
    processInfo.RedirectStandardError = true;
    processInfo.RedirectStandardOutput = true;


    process = Process.Start(processInfo);
    process.WaitForExit(5000);

    // *** Read the streams ***
    string output = process.StandardOutput.ReadToEnd();
    string error = process.StandardError.ReadToEnd();

    int exitCode = process.ExitCode;
    process.Close();
    return new NuGetBO.ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output };
}

但我收到以下错误:

"Access is denied.\r\nfatal: Not a git repository (or any of the parent directories): .git\n"

换句话说,我已经尝试从 C# 执行我的批处理文件几个小时但没有成功,但是,如果我在命令行中执行它,我的批处理文件可以正常工作。

如何让我的 C# 函数正常工作,我应该在批处理文件或 C# 函数中更改哪些内容?谢谢

编辑:

我已将批处理文件的内容更改为以下内容:

cd <path to my git repository> && git pull

但我收到以下错误:

Access is denied.

编辑2:

我已添加对该文件夹的访问权限,但现在出现新错误:

fatal: Not a git repository: "../.git/modules/myProject\n"

编辑3:

我已授予 EDIT2 中提到的文件夹的访问权限,但现在收到以下错误:

"error: unable to create directory for C:/myFolder/.git/modules/myProject/ORIG_HEAD\nfatal: Cannot lock the ref 'ORIG_HEAD'.\n"

最佳答案

您需要将 processInfo.WorkingDirectory 设置为 git 存储库的位置。

关于c# - C# 函数执行批处理文件的 Git pull 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13407220/

相关文章:

file - 保存 curl 下载统计/日志

java - 从命令行运行 java,文件夹结构和包名称不匹配

c# - 为什么 C# 不允许我调用 void 方法作为返回语句的一部分?

c# - 析构函数中的紧密联系

C# LINQ SEO 一般编程题,如何正确获取某个位置的结果?

ruby - 关于Grit的几个问题

c# - 页面渲染和 http 处理程序

git - 在 Windows 上 checkout HEAD(和变体)时奇怪的 Git 行为

git fetch上游不允许我在Windows上使用公钥

linux - 如何从命令行重置 Jenkins 安全设置?