3

正如这个 SU 回答所指出的,为了更改文件夹的图标,必须将文件夹的属性更改为只读或系统,并使其desktop.ini包含类似

[.ShellClassInfo]
IconResource=somePath.dll,0

win32api.SetFileAttributes(dirpath, win32con.FILE_ATTRIBUTE_READONLY)虽然从头开始使用和创建它很简单desktop.ini,但我想保留可能存在的其他自定义项desktop.ini。但是我应该为此使用ConfigParser还是例如win32api(或也许ctypes.win32)提供本机方法来这样做?

4

1 回答 1

1

好的,所以从这个线程中,我设法得到了一些工作。我希望它会帮助你。

这是我的基本 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

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

于 2015-02-13T11:16:31.690 回答