为什么DefaultIcon注册表设置不起作用(Python/winreg)
下面的代码旨在根据我所了解的注册表设置,为文件类型设置默认图标和默认应用程序。我可以看到正在使用regedit进行更改,但是具有该扩展名的文件的信息没有更改。有什么建议吗为什么DefaultIcon注册表设置不起作用(Python/winreg),python,windows,registry,winreg,Python,Windows,Registry,Winreg,下面的代码旨在根据我所了解的注册表设置,为文件类型设置默认图标和默认应用程序。我可以看到正在使用regedit进行更改,但是具有该扩展名的文件的信息没有更改。有什么建议吗 import os.path import _winreg as winreg G2path="Z:\\Scratch\\GSASII" G2icon = 'c:\\gsas2.ico,0' G2bat = os.path.join(G2path,'RunGSASII.bat') gpxkey = winreg.Creat
import os.path
import _winreg as winreg
G2path="Z:\\Scratch\\GSASII"
G2icon = 'c:\\gsas2.ico,0'
G2bat = os.path.join(G2path,'RunGSASII.bat')
gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
#gpxkey = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, '.gpx')
iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
openkey = winreg.CreateKey(gpxkey, 'OpenWithList')
g2key = winreg.CreateKey(openkey, 'GSAS-II')
winreg.SetValue(g2key, None, winreg.REG_SZ, G2bat)
winreg.CloseKey(iconkey)
winreg.CloseKey(g2key)
winreg.CloseKey(openkey)
#winreg.CloseKey(gpxkey)
winreg.FlushKey(gpxkey)
在此处找到了解决我的图标问题的线索:。我需要:
import os.path
import _winreg as winreg
G2path="Z:\\Scratch\\GSASII"
G2icon = 'c:\\gsas2.ico'
G2bat = os.path.join(G2path,'RunGSASII.bat')
gpxkey = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, '.gpx')
winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project') # what was needed!
iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
winreg.CloseKey(iconkey)
winreg.CloseKey(gpxkey)
# show the change
import win32com.shell.shell, win32com.shell.shellcon
win32com.shell.shell.SHChangeNotify(
win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)
为了获得与扩展关联的应用程序,我需要做更多的工作。这有助于:
请注意,正如链接中所建议的,我使用的是HKEY_CURRENT_用户,因为这样可以避免需要管理员权限
import _winreg as winreg
G2bat = "Z:\\Scratch\\GSASII\\RunGSASII.bat"
G2icon = 'c:\\gsas2.ico'
gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\.gpx')
winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II.project')
winreg.CloseKey(gpxkey)
gpxkey = winreg.CreateKey(winreg.HKEY_CURRENT_USER,r'Software\CLASSES\GSAS-II.project')
winreg.SetValue(gpxkey, None, winreg.REG_SZ, 'GSAS-II project')
iconkey = winreg.CreateKey(gpxkey, 'DefaultIcon')
winreg.SetValue(iconkey, None, winreg.REG_SZ, G2icon)
openkey = winreg.CreateKey(gpxkey, r'shell\open\command')
winreg.SetValue(openkey, None, winreg.REG_SZ, G2bat+" %1")
winreg.CloseKey(iconkey)
winreg.CloseKey(openkey)
winreg.CloseKey(gpxkey)
import win32com.shell.shell, win32com.shell.shellcon
win32com.shell.shell.SHChangeNotify(
win32com.shell.shellcon.SHCNE_ASSOCCHANGED, 0, None, None)