简体   繁体   English

如何以编程方式在 Windows 上按名称打开应用程序?

[英]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.

相关问题 如何在 macOS 上按名称以编程方式打开应用程序? - How to programmatically open an application by name on macOS? 如何以编程方式检测Windows中已崩溃的应用程序? - How to programmatically detect an application has crashed in Windows? 如何以编程方式检查Windows计算机上的打开浏览器实例? - How do I programmatically check for open browser instances on a Windows machine? 当不是活动应用程序(窗口)时,如何以编程方式引发 Qt 窗口/小部件? - How to programmatically raise a Qt Window/Widget when not the active application (windows)? Python - 如何使用用户名和密码打开 Windows 共享 - Python - How to open Windows share using user name and password 如何使用__name __ =='__main__'在Linux和Windows上运行python应用程序 - how to run python application on Linux and Windows with if __name__== '__main__' 如何在Windows 10中以编程方式调整过程窗口的大小? - How to programmatically resize process windows in Windows 10? Windows任务栏(Windows 7?) - 如何在“控制面板”通知对话框中设置应用程序名称 - Windows taskbar (Windows 7?) — how to set the application name in the Control Panel notifications dialog Python 关闭 windows 中所有打开的应用程序的代码 - Python code to close all open application in windows Windows“使用” python py2exe应用程序“打开” - Windows “open with” python py2exe application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM