Usando Python:
EXT, EXT_TYPE = ".xyz", "XYZ file"
EXE_PATH = r"path\to\my\exe"
# %L is the long (full path) version of path
extCmd = '"%s" "%%L" %%*' % EXE_PATH
# Using assoc and ftype easier than editing registry!
assert os.system('assoc %s=%s' % (EXT, EXT_TYPE))==0
assert os.system('ftype %s=%s' % (EXT_TYPE, extCmd))==0
Asociar un icono con el tipo de extensión:
import _winreg
try:
ext = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, EXT_TYPE)
_winreg.SetValue(ext, "DefaultIcon", _winreg.REG_SZ, ICON_PATH)
_winreg.CloseKey(ext)
except WindowsError:
print "Error associating icon"
registrar la extensión como un tipo de ejecutable (es decir PATHEXT):
try:
key = r'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
# get current value
ext = _winreg.OpenKey(reg, key)
pathext = _winreg.QueryValueEx(ext, 'PATHEXT')[0]
if not EXT in pathext:
_winreg.CloseKey(ext)
# modify the current value
ext = _winreg.OpenKey(reg, key, 0, _winreg.KEY_ALL_ACCESS)
pathext += ';' + EXT
_winreg.SetValueEx(ext, 'PATHEXT', 0, _winreg.REG_SZ, pathext)
_winreg.CloseKey(ext)
_winreg.CloseKey(reg)
except WindowsError:
print "Error adding to PATHEXT"
Además, para obtener reconocimiento de PATHEXT sin iniciar sesión de nuevo, puede actualizar el entorno: (gracias a Enthough t para este)
def refreshEnvironment():
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
sParam = "Environment"
import win32gui
res1, res2 = win32gui.SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, sParam, SMTO_ABORTIFHUNG, 100)
tengo este código: 'Proceso EmulatorProcess; EmulatorProcess = Process.Start (@ "C: \ Documents and Settings \ serg \ Desktop \ Emubox v0.01 \ SuperNintendo \ Roms \ Super Mario RPG (U) .smc"); EmulatorProcess.WaitForInputIdle(); ' Utilicé un camino codificado solo para probar cosas. ¿Cómo podría aplicar su última línea de código a mis necesidades? ¡Gracias! : D –
De [esta respuesta] (http://stackoverflow.com/a/13535039/1497596): * "La clase' RegistryKey' implementa 'IDisposable' y entonces debe envolver sus claves en una declaración' using'. " * O, como alternativa, debe llamar a [Cerrar o desechar] (http://stackoverflow.com/a/37059244/1497596) en el 'RegistryKey' cuando haya terminado con él. Esto significa que incluir 'CreateSubKey' en una cadena de métodos como se muestra en este ejemplo es una mala idea. – DavidRR