Jaap Joris Vens
Jaap Joris Vens

Reputation: 3550

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. 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.

On Linux, I can simply do:

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

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

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. 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?

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

Upvotes: 1

Views: 414

Answers (1)

Nullman
Nullman

Reputation: 4279

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

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

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

Upvotes: 2

Related Questions