首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么DefaultIcon注册表设置无效(Python/winreg)

为什么DefaultIcon注册表设置无效(Python/winreg)
EN

Stack Overflow用户
提问于 2015-05-16 04:19:58
回答 1查看 624关注 0票数 1

下面的代码旨在根据我对注册表设置的了解,为文件类型设置默认图标和默认应用程序。我可以看到更改是使用regedit进行的,但具有该扩展名的文件的信息并未更改。对如何做到这一点有什么建议吗?

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2015-05-17 12:21:00

在这里可以找到解决我的图标问题的线索:https://msdn.microsoft.com/en-us/library/windows/desktop/hh127427%28v=vs.85%29.aspx。我需要:

  1. 为我正在创建的键设置一个值,
  2. 触发shell的更新以查看更改。

下面的代码可以让图标显示出来:

代码语言:javascript
复制
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)

要获得与该扩展相关联的应用程序,我需要做更多的工作。这很有帮助:Create registry entry to associate file extension with application in C++

请注意,正如链接中所建议的,我使用的是HKEY_CURRENT_USER,因为这避免了需要管理员权限。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30268075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档