【问题标题】:How to customize a folder's icon via Python?如何通过 Python 自定义文件夹的图标?
【发布时间】:2015-01-02 18:12:45
【问题描述】:

正如this SU answer 所说,要更改文件夹的图标,必须将文件夹的属性更改为只读或系统,并使其desktop.ini 包含类似的内容

[.ShellClassInfo]
IconResource=somePath.dll,0

虽然使用win32api.SetFileAttributes(dirpath, win32con.FILE_ATTRIBUTE_READONLY) 并从头开始创建desktop.ini 很简单,但我想保留可能存在的desktop.ini 中存在的其他自定义设置。但是我应该为此使用ConfigParser 还是使用例如win32api(或者可能是ctypes.win32)提供本地方法来做到这一点?

【问题讨论】:

  • (目前我发现的唯一一个自定义相关的功能是SHSetLocalizedName)
  • 你好,由于这个帖子不是那么老,我允许自己问你是否找到了答案。我目前面临同样的问题。
  • @DrHaze 不幸的是,到目前为止,我一直坚持使用 desktop.ini ......也许在这个问题上的小额奖励有助于激励某人报告他们的发现

标签: python windows desktop.ini


【解决方案1】:

好的,所以从this thread,我设法得到了一些工作。希望对你有帮助。

这是我的基本 desktop.ini 文件:

[.ShellClassInfo]
IconResource=somePath.dll,0

[Fruits]
Apple = Blue
Strawberry = Pink

[Vegies]
Potatoe = Green
Carrot = Orange

[RandomClassInfo]
foo = somePath.ddsll,0

这是我使用的脚本:

from ConfigParser import RawConfigParser

dict = {"Fruits":{"Apple":"Green", "Strawberry":"Red"},"Vegies":{"Carrot":"Orange"}  }
# Get a config object
config = RawConfigParser()
# Read the file 'desktop.ini'
config.read(r'C:\Path\To\desktop.ini')

for section in dict.keys():
    for option in dict[section]:
        try:
            # Read the value from section 'Fruit', option 'Apple'
            currentVal = config.get( section, option )
            print "Current value of " + section + " - " + option + ": " + currentVal
            # If the value is not the right one
            if currentVal != dict[section][option]:
                print "Replacing value of " + section + " - " + option + ": " + dict[section][option]
                # Then we set the value to 'Llama'
                config.set( section, option, dict[section][option])
        except:
            print "Could not find " + section + " - " + option 

# Rewrite the configuration to the .ini file
with open(r'C:\Path\To\desktop.ini', 'w') as myconfig:
    config.write(myconfig)

这是输出的 desktop.ini 文件:

[.ShellClassInfo]
iconresource = somePath.dll,0

[Fruits]
apple = Green
strawberry = Red

[Vegies]
potatoe = Green
carrot = Orange

[RandomClassInfo]
foo = somePath.ddsll,0

我唯一的问题是选项丢失了它们的首字母大写。

【讨论】:

  • 刚刚测试,大小写丢失不影响iconresource的行为。
  • 我认为问题是我在网络共享上尝试过这个,但由于我的设置同时更改,我无法重现错误
  • 小写问题可以通过config.optionxform = str修复
  • 我确实基于此构建了一个脚本:github.com/zommuter/desktopini.py
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 2021-11-16
  • 2012-10-19
  • 1970-01-01
  • 2020-10-27
  • 2014-06-08
相关资源
最近更新 更多