2014-11-06 80 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

相關問題