PowerShell刷新Windows图标缓存


有时,Windows 资源管理器显示图标有问题。比如我们升级了PowerShell 5.0以后,PowerShell和PowerShell ISE 的图标被更新成了modern风格。如果你看到的还是旧的图标,那可能就是缓存问题导致的。这里分享一个函数,可以帮助我们通过调用原生态的Windows API,强制刷新Windows图标缓存。

function Update-ExplorerIcon {
[CmdletBinding()]
param()

$code = @'
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff); 
private const int WM_SETTINGCHANGE = 0x1a; 
private const int SMTO_ABORTIFHUNG = 0x0002; 

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam,
IntPtr lParam);

[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)] 
private static extern IntPtr SendMessageTimeout ( IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult ); 


[System.Runtime.InteropServices.DllImport("Shell32.dll")] 
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);


public static void Refresh() {
SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero); 
}
'@

Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name Explorer 
[MyWinAPI.Explorer]::Refresh()

}

原文链接:Refreshing Icon Cache

 

×用微信扫描并分享
本文链接: https://www.pstips.net/refreshing-icon-cache.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

电子邮件地址不会被公开。 必填项已用*标注