【发布时间】:2015-01-14 17:46:01
【问题描述】:
我已经用 C# 编写了一个 .exe 文件,如果我手动启动它(双击)它可以正常工作,但每次我尝试用 python 打开它。
import os
os.system('"D:\\XX\MyFile.exe"')
我也试过这个:
import subprocess
exefile = 'D:\\XX\\MyFile.exe'
subprocess.call([exefile])
程序立即崩溃,提示“MyFile.exe 已停止工作。某个程序导致程序停止正常工作。请关闭该程序。”。
现在让我感到困惑的是,如果我手动运行它,但当我尝试通过 Python 启动它时它会失败。
C# 应用程序只是一个更新程序,我编写它来检查我的 pythonscripts 的更新。它有一个 .dll,其中包含更新例程和加载 (.dat) 文件以获取信息的表单。
有什么我想念的吗?
编辑:
经过一些修改和诊断,我的 exefile 现在启动了,但我意识到如果它是由 Python 启动的,exe 无法在我的 C# 应用程序中执行以下功能。
private UpdateSaveFile DecodeSaveFile(string LocalUpdateFile)
{
FileStream localFileStream = null;
BinaryFormatter decoder = null;
try
{
localFileStream = File.Open(LocalUpdateFile, FileMode.Open, FileAccess.Read);
decoder = new BinaryFormatter();
return (UpdateSaveFile)decoder.Deserialize(localFileStream);
}
catch (Exception e)
{
throw new InvalidDataException("The local update info file is corrupt!", e);
}
finally
{
if (localFileStream != null)
localFileStream.Dispose();
}
}
现在,每当我从 python 运行 exefile 时,它都会向我抛出“本地更新信息文件已损坏”。但是,如果我手动运行 exefile(不使用 python)/cmd 提示符,它工作得非常好。
有人知道吗?是因为管理员权限吗? python启动的exe有管理员权限吗?
【问题讨论】:
-
有什么理由你用
exefile = '%s' % 'D:\\XX\\MyFile.exe'而不是exefile = 'D:\\XX\\MyFile.exe'吗? -
在第一个代码块中有四个引号对我来说似乎很奇怪。你试过
os.system("D:\\XX\MyFile.exe")吗? -
我觉得
os.system(r'D:\XX\MyFile.exe')更好 -
为什么不只是
import subprocess subprocess.Popen([r"MyFile.exe"]?你能试试这个吗 -
我尝试了所有上述 cmets,但都是一样的 :( 我是否必须为 exe 指定任何内容?因为我的程序有一个 .dll 并且它可能不会运行?