我正在尝试启动一个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:‘访问被拒绝’“。环顾四周,我看到的建议是以管理员身份运行,但这不是一个选项。有没有办法在没有那个的情况下做到这一点?运行代码的用户有权运行git-bash。
编辑1:
我开始研究使用一个.BAT文件,但是为了达到这个目的,我需要使用一个bat,第二个bat文件,它激活了虚拟环境,导致它不能运行bat文件的第二部分。无论如何,让它在同一命令提示符上执行这两个命令可以解决这个问题。
发布于 2021-06-09 04:07:31
我使用一个bat文件解决了这个问题,该文件不需要管理员权限即可从C#代码运行。以及使用call命令从批处理文件执行另一个批处理文件。我还使用了另一个SO post来使用批处理文件中的相对路径文件。
https://stackoverflow.com/questions/67889320
复制相似问题