2014-11-06 76 views
2

由于this SU answer指出,为了改变文件夹的图标,一个有可能改变一个文件夹的属性或系统只读,并有其desktop.ini包含类似如何通过Python自定义文件夹的图标?

[.ShellClassInfo] 
IconResource=somePath.dll,0 

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

+0

(目前我发现的唯一与自定义相关的函数是[SHSetLocalizedName](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762250%28v=vs.85%29.aspx )) – 2014-11-06 09:45:49

+0

你好,因为这个线程不是那么古老,我允许自己问你是否找到答案。我目前面临同样的问题。 – DrHaze 2015-02-12 16:10:36

+0

@DrHaze不幸的是,我从零开始坚持使用desktop.ini ......可能在这个问题上的小小赏金有助于激励某人报告他们的发现 – 2015-02-12 17:33:59

回答

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 

我唯一的问题是,这些选项将丢失他们的第一个字母大写字母。

+0

经过测试,大小写不会影响iconresource的行为。 – DrHaze 2015-02-13 11:23:52

+0

我认为问题在于我在网络共享上试过这个,但是由于我的设置在同一时间发生了变化,所以我无法重现错误 – 2015-08-17 09:16:09

+1

小写问题可以通过['config.optionxform = str'](https:// stackoverflow的.com /一个/321973分之1611877) – 2016-03-03 15:04:30

相关问题