[英]Is there a way to only update one folder's desktop.ini?
我正在制作一個涉及我 windows 桌面上文件夾圖標的項目。 當我用 python 更改圖標時,更改不會立即發生。 您必須以某種方式刷新它(例如重新啟動筆記本電腦)。 為了解決這個問題,我搜索了 inte.net 並從這個 github 頁面找到了這段代碼: https://github.com/sunshowers/iconrefresher/blob/master/refresh-icons.py
def update_folder_icon():
# !/usr/bin/env python
# Released to the public domain.
# http://creativecommons.org/publicdomain/zero/1.0/
import ctypes
from ctypes import wintypes
# http://msdn.microsoft.com/en-us/library/ms644950
SendMessageTimeout = ctypes.windll.user32.SendMessageTimeoutA
SendMessageTimeout.restype = wintypes.LPARAM # aka LRESULT
SendMessageTimeout.argtypes = [wintypes.HWND, wintypes.UINT, wintypes.WPARAM, wintypes.LPARAM,
wintypes.UINT, wintypes.UINT, ctypes.c_void_p]
# http://msdn.microsoft.com/en-us/library/bb762118
SHChangeNotify = ctypes.windll.shell32.SHChangeNotify
SHChangeNotify.restype = None
SHChangeNotify.argtypes = [wintypes.LONG, wintypes.UINT, wintypes.LPCVOID, wintypes.LPCVOID]
HWND_BROADCAST = 0xFFFF
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 0x0002
SHCNE_ASSOCCHANGED = 0x08000000
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 0, SMTO_ABORTIFHUNG, 5000, None)
SHChangeNotify(SHCNE_ASSOCCHANGED, 0, None, None)
每次我使用此代碼時,所有文件夾圖標都會刷新並閃爍。 我需要在我的項目中經常使用此代碼(每 3 秒一次),所以最好不要每次都看到閃爍效果。 所以我的問題是:有沒有辦法讓我只更新一個特定的文件夾,例如將文件夾路徑作為參數傳遞給update_folder_icon()
function?
def set_folder_icon(folder_path, icon_path):
if not os.path.isdir(folder_path):
print("Folder Required To Set The Icon!")
return
shell32 = ctypes.windll.shell32
folder_path = os.path.abspath(folder_path)
icon_path = os.path.abspath(icon_path)
fcs = SHFOLDERCUSTOMSETTINGS()
fcs.dwSize = sizeof(fcs)
fcs.dwMask = FCSM_ICONFILE
fcs.pszIconFile = icon_path
fcs.cchIconFile = 0
fcs.iIconIndex = 0
hr = shell32.SHGetSetFolderCustomSettings(byref(fcs), folder_path, FCS_FORCEWRITE)
if hr:
raise WindowsError(win32api.FormatMessage(hr))
sfi = SHFILEINFO()
hr = shell32.SHGetFileInfoW(folder_path, 0, byref(sfi), sizeof(sfi),
SHGFI_ICONLOCATION)
if hr == 0:
raise WindowsError(win32api.FormatMessage(hr))
shell32.SHUpdateImageW(sfi.szDisplayName, sfi.iIcon, 0, 0)
我設法解決了這個問題。 問題並不是真正的代碼本身,它更像是一個 windows 錯誤。 當您第一次設置文件夾的圖標時它起作用,但是當您再次更改圖標時,但圖標文件具有相同的名稱然后更改不會發生。 因此,為了解決這個問題,您要么必須采取一些激進的措施,例如SHCNE_ASSOCCHANGED
,要么只使用不同的圖標文件名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.