您的位置:首页 > 其它

强制刷新图标缓存

2015-11-25 14:36 211 查看
开发Mac平台的应用程序时遇到一个热更新图标的需求。

Mac的应用程序是
.app
bundle,图标文件放在
test.app/Contents/Resources/
路径,在
test.app/Contents/Info.plist
中指定。

可是,更换
.icns
图标文件后,Finder里不即时更新显示。如何绕开缓存机制,强制刷新显示图标呢?当然,得是以编程方式实现。

搜到一些建议。

重建Launch Services数据库,

lsregister -kill -seed -r
lsregister -f <.app path>


无效。

修改bundle的时间戳。

touch /Applications/App.app


无效。

有人提到“创建-删除”一个文件的方法,有人尝试了这个方法,告诉大家说,他尝试了所有bundle里的目录都无效,除了
test.app/
。测试有效。谢谢他。

Qt测试代码,创建一个文件夹,再删除,

QDir junk;
junk.mkdir(strBundlePath + "/junk");
junk.rmdir(strBundlePath + "/junk");


可是紧接着在其他电脑上测试发现,有时还是无效。

研究发现,无效时,
test.app/
bundle目录里有一个文件
Icon?


它其实是
Icon\r
,终端里的自动补全会显示为
Icon^M
。这个文件是干吗用的?怎么产生的?这里有详细介绍

说是改变文件夹图标时会产生这个文件,实测发现,只在右键
test.app
bundle选择“Get Info”,拖动图片文件到Info对话框左上角图标位置时,会产生这个文件。不知道有没有其他动作也会。

有这个文件的时候,前面说的“创建-删除”文件夹/文件方法无效,可能的原因是“when you change the icon, it is not actually applied to the folder itself but rather to the 'Icon\r' file inside the folder”。

解决方案是删除它。测试代码如下,

QFile jfile(strBundlePath + "/Icon\r");
if(jfile.exists())
jfile.remove();
QDir junk; junk.mkdir(strBundlePath + "/junk"); junk.rmdir(strBundlePath + "/junk");


项目里加上这样的代码真是无奈,所谓笨办法吧。

Windows平台上,新图标编译在
.exe
文件里,可是
.exe
文件热更新后,桌面的快捷方式图标一样存在缓存不能立刻刷新的问题。思路类似,没再尝试,举例如下,来自这篇博客

rem 关闭Windows外壳程序explorer
taskkill /f /im explorer.exe
rem 清理系统图标缓存数据库
attrib -h -s -r "%userprofile%\AppData\Local\IconCache.db"
del /f "%userprofile%\AppData\Local\IconCache.db"
attrib /s /d -h -s -r "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\*"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_32.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_96.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_102.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_256.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_1024.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_idx.db"
del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_sr.db"
rem 清理 系统托盘记忆的图标
echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams
echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream
rem 重启Windows外壳程序explorer
start explorer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: