我已经使用 Python2.7 创建了一个脚本,并使用 pyinstaller 将其编译为同名的 exe,在这种情况下,“GeneralStats.py”使用 --onefile 和 -w 参数变为“GeneralStats.exe”。当用 C# 调用时,我使用:var pythonDirectory = (Directory.GetCurrentDirectory());var filePathExe1 = Path.Combine(pythonDirectory + "\\Python\\GeneralStats.exe");Process.Start(filePathExe1);在 C# 外部调用时,因此在我的本地文件中我可以运行 .exe,结果是一个文本文件,其中包含许多值(正确运行)。但是,当以这种格式使用 C# 运行时,出现“GeneralStats 返回 -1!”的错误消息。我以前遇到过问题,但这是一个简单的 python 错误,当我返回到我的代码并运行它时,我会收到一个我忽略的错误。这次我的 python 代码没有返回错误并且在 C# 之外工作。为什么会这样的任何想法?我可以提供任何必要的代码或文件目录,请询问您是否认为它有助于调试。编辑:通过删除解决:var filePathExe1 = Path.Combine(pythonDirectory + "\\Python\\GeneralStats.exe");Process.Start(filePathExe1);并替换为:ProcessStartInfo _processStartInfo = new ProcessStartInfo();_processStartInfo.WorkingDirectory = Path.Combine(pythonDirectory + "\\Python");_processStartInfo.FileName = @"GeneralStats.exe";_processStartInfo.CreateNoWindow = true;Process myProcess = Process.Start(_processStartInfo);
1 回答

互换的青春
TA贡献1797条经验 获得超6个赞
您需要为其设置工作目录Process
- 它可能正在尝试从其工作目录加载文件但没有找到它们。
见,例如这个:
ProcessStartInfo.WorkingDirectory
在启动进程之前使用该属性进行设置。如果未设置该属性,则默认工作目录为%SYSTEMROOT%\system32
.
将其设置为路径 where GeneralStats.exe
。
添加回答
举报
0/150
提交
取消