[英]How to programmatically open an application by name on Windows?
I'm writing a cross-platform Python application that acts as a frontend for DOSBox .我正在编写一个跨平台的 Python 应用程序,它充当DOSBox的前端。 It needs to call the DOSBox executable with a number of command line arguments.
它需要调用带有多个命令行arguments的DOSBox可执行文件。 I don't want to hardcode a specific path to DOSBox because it might depend on where the user has installed it.
我不想硬编码 DOSBox 的特定路径,因为它可能取决于用户安装它的位置。
On Linux, I can simply do:在 Linux 上,我可以简单地做:
import subprocess
subprocess.run(['dosbox'] + args)
On Windows, however, I currently use the following code:然而,在 Windows 上,我目前使用以下代码:
import subprocess
subprocess.run(['C:\PROGRAM FILES(X86)\DOSBOX\DOSBOX.EXE'] + args)
Which seems awfully specific, and doesn't work if DOSBox is installed in, for instance, the %LOCALAPPDATA%\Programs
folder.这似乎非常具体,如果 DOSBox 安装在例如
%LOCALAPPDATA%\Programs
文件夹中,则它不起作用。 Instead, I'd like to query the registry for the correct entry point to a program that identifies as "DOSBox".相反,我想查询注册表以获得标识为“DOSBox”的程序的正确入口点。 How can I do that in Python?
我该如何在 Python 中做到这一点?
(NB: I have also asked this sibling question for macOS.) (注意:我也为 macOS 提出了这个兄弟问题。)
You will probably have to use winreg
to find where dosbox is installed您可能必须使用
winreg
来查找 dosbox 的安装位置
I'm basing my answer on this answer我的答案基于这个答案
import winreg
reg_conn = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
# note that i dont have dosbox so you may have to double check the capitalization
key = winreg.OpenKey(reg_conn , r"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DOSBox")
res_tuple = winreg.QueryValueEx(key, 'InstallLocation')
res_tuple[0]
should be the directory of where dosbox is installed, but as i've mentioned I do not have it installed so you should double check that InstallLocation
is the correct key res_tuple[0]
应该是安装 dosbox 的目录,但正如我所提到的,我没有安装它,所以你应该仔细检查InstallLocation
是否是正确的键
This will probably not work for the portable version of dosbox because it probably doesnt have any registry keys这可能不适用于 dosbox 的便携式版本,因为它可能没有任何注册表项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.