Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么DefaultIcon注册表设置不起作用(Python/winreg)_Python_Windows_Registry_Winreg - Fatal编程技术网

为什么DefaultIcon注册表设置不起作用(Python/winreg)

为什么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

下面的代码旨在根据我所了解的注册表设置,为文件类型设置默认图标和默认应用程序。我可以看到正在使用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.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)

在此处找到了解决我的图标问题的线索:。我需要:

  • 为我创建的键设置一个值,然后
  • 触发shell的更新以查看更改 此代码用于显示图标:

    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)