開發Mac平臺的應用程序時遇到一個熱更新圖標的需求。html
Mac的應用程序是.app
bundle,圖標文件放在test.app/Contents/Resources/
路徑,在test.app/Contents/Info.plist
中指定。數據庫
但是,更換.icns
圖標文件後,Finder裏不即時更新顯示。如何繞開緩存機制,強制刷新顯示圖標呢?固然,得是以編程方式實現。編程
搜到一些建議。緩存
重建Launch Services數據庫,app
lsregister -kill -seed -r lsregister -f <.app path>
無效。ide
修改bundle的時間戳。測試
touch /Applications/App.app
無效。spa
有人提到「建立-刪除」一個文件的方法,有人嘗試了這個方法,告訴你們說,他嘗試了全部bundle裏的目錄都無效,除了test.app/
。測試有效。謝謝他。code
Qt測試代碼,建立一個文件夾,再刪除,htm
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