Reputation: 848
The code below is intended to set the default icon and default application for a file type based on what I have been able to learn about Registry settings. I can see that the changes are being made using regedit, but the information for the files with that extension are not changing. Any suggestions on how to do this?
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)
Upvotes: 1
Views: 907
Reputation: 848
Clues for solving my icon problem were found here: https://msdn.microsoft.com/en-us/library/windows/desktop/hh127427%28v=vs.85%29.aspx. I needed to:
This code does work to get an icon displayed:
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)
To also get an application associated with the extension, I needed to do more. This helped: Create registry entry to associate file extension with application in C++
Note that as suggested in the link, I am using HKEY_CURRENT_USER, since that avoids requiring admin privs.
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)
Upvotes: 1