【问题标题】:Use Git-bash from C# without Admin permissions在没有管理员权限的情况下从 C# 使用 Git-bash
【发布时间】:2021-08-25 13:59:00
【问题描述】:

我试图启动一个 python 虚拟环境并使用以下代码从 C# 文件运行一个 python 文件。

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

运行时,我收到一条错误消息“System.ComponentModel.Win32Exception: 'Access is denied.'” 环顾我看到的建议是以管理员身份运行,但这不是一个选项。没有那个有没有办法做到这一点?运行代码的用户有运行git-bash的权限。

编辑 1:

我开始研究使用 .BAT 文件,但要做到这一点,我需要使用 bat 激活虚拟环境的第二个 bat 文件,这导致它无法运行 bat 文件的第二部分。无论如何让它在同一个命令提示符下执行这两个命令将解决问题。

【问题讨论】:

    标签: python c# batch-file git-bash


    【解决方案1】:

    我使用 bat 文件解决了这个问题,该文件不需要管理员权限即可从 C# 代码运行。并使用 call 命令从批处理文件中执行另一个批处理文件。我还使用另一个 SO 帖子在批处理文件中使用相对路径文件。

    relative path in BAT script

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 2014-03-17
      • 2021-07-10
      • 2012-04-20
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多