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:
- set a value for the key I was creating and
- trigger an update of the shell to see the changes.
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)