如何在 Windows 上以编程方式按名称打开应用程序? [英] How to programmatically open an application by name on Windows?

查看:29
本文介绍了如何在 Windows 上以编程方式按名称打开应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个跨平台 Python 应用程序,用作 DOSBox 的前端.它需要使用许多命令行参数调用 DOSBox 可执行文件.我不想硬编码 DOSBox 的特定路径,因为它可能取决于用户安装它的位置.

I'm writing a cross-platform Python application that acts as a frontend for DOSBox. It needs to call the DOSBox executable with a number of command line arguments. I don't want to hardcode a specific path to DOSBox because it might depend on where the user has installed it.

在 Linux 上,我可以简单地做:

On Linux, I can simply do:

import subprocess
subprocess.run(['dosbox'] + args)

然而,在 Windows 上,我目前使用以下代码:

On Windows, however, I currently use the following code:

import subprocess
subprocess.run(['C:\PROGRAM FILES(X86)\DOSBOX\DOSBOX.EXE'] + args)

这看起来非常具体,如果 DOSBox 安装在例如 %LOCALAPPDATA%\Programs 文件夹.相反,我想在注册表中查询标识为DOSBox"的程序的正确入口点.我怎样才能在 Python 中做到这一点?

Which seems awfully specific, and doesn't work if DOSBox is installed in, for instance, the %LOCALAPPDATA%\Programs folder. Instead, I'd like to query the registry for the correct entry point to a program that identifies as "DOSBox". How can I do that in Python?

(注意:我还针对 macOS 提出了这个兄弟问题.)

(NB: I have also asked this sibling question for macOS.)

推荐答案

您可能需要使用 winreg 来查找安装 dosbox 的位置

You will probably have to use winreg to find where dosbox is installed

我的回答基于这个答案

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] 应该是安装 dosbox 的目录,但正如我提到的,我没有安装它,所以你应该仔细检查 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

这可能不适用于便携式版本的 dosbox,因为它可能没有任何注册表项

This will probably not work for the portable version of dosbox because it probably doesnt have any registry keys

这篇关于如何在 Windows 上以编程方式按名称打开应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆