有没有办法只更新一个文件夹的desktop.ini?
Is there a way to only update one folder's desktop.ini?
我正在制作一个涉及 windows 桌面上文件夹图标的项目。当我使用 python 更改图标时,更改不会立即发生。您必须以某种方式刷新它(例如重新启动笔记本电脑)。为了解决这个问题,我搜索了互联网并从 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()
函数?
这是我用来更改文件夹图标的代码:
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版本:Windows11
- Python版本:3.8
- IDE: Pycharm
我设法解决了这个问题。问题并不是真正的代码本身,它更像是一个 windows 错误。当您第一次设置文件夹的图标时它起作用,但是当您再次更改图标时,但图标文件具有相同的名称然后更改不会发生。因此,为了解决这个问题,您要么必须采取一些积极的措施,例如 SHCNE_ASSOCCHANGED
,要么只使用不同的图标文件名。
我正在制作一个涉及 windows 桌面上文件夹图标的项目。当我使用 python 更改图标时,更改不会立即发生。您必须以某种方式刷新它(例如重新启动笔记本电脑)。为了解决这个问题,我搜索了互联网并从 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()
函数?
这是我用来更改文件夹图标的代码:
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版本:Windows11
- Python版本:3.8
- IDE: Pycharm
我设法解决了这个问题。问题并不是真正的代码本身,它更像是一个 windows 错误。当您第一次设置文件夹的图标时它起作用,但是当您再次更改图标时,但图标文件具有相同的名称然后更改不会发生。因此,为了解决这个问题,您要么必须采取一些积极的措施,例如 SHCNE_ASSOCCHANGED
,要么只使用不同的图标文件名。