2014-05-15 54 views
0

我正在編寫一個程序,它爲Windows文件夾創建縮略圖圖標。我想給用戶一個簡單的方法來刷新他們的縮略圖緩存。以編程方式清理Windows縮略圖緩存

是否有可能通過任何Windows API做到這一點?理想情況下在C#中,但更可能通過包裝一些C++。

如果沒有,是否有批准的方式來確定用戶縮略圖緩存的位置並手動刪除它們。或者最好只是推出cleanmgr並讓用戶從那裏做到這一點?

回答

1

您可以爲模擬cleanmgr工具創建窗口縮略圖清潔擴展的主機。

type 
    TThumbnailCleanerCallBack = class(TInterfacedObject, IEmptyVolumeCacheCallBack) 
    private 
    function ScanProgress(dwlSpaceUsed: UInt64; dwFlags: DWORD; pcwszStatus: LPCWSTR): HRESULT; stdcall; 
    function PurgeProgress(dwlSpaceFreed, dwlSpaceToFree: UInt64; dwFlags: DWORD; pcwszStatus: LPCWSTR): HRESULT; stdcall; 
    end; 

function TThumbnailCleanerCallBack.ScanProgress(dwlSpaceUsed: UInt64; dwFlags: DWORD; pcwszStatus: LPCWSTR): HRESULT; 
begin 
    Result := S_OK; 
end; 

function TThumbnailCleanerCallBack.PurgeProgress(dwlSpaceFreed, dwlSpaceToFree: UInt64; dwFlags: DWORD; pcwszStatus: LPCWSTR): HRESULT; 
begin 
    Result := S_OK; 
end; 

procedure CleanThumbnailCache; 
const 
    ThumbnailCleanerCLSID: TGUID = '{889900c3-59f3-4c2f-ae21-a409ea01e605}'; 
var 
    Drives: DWORD; 
    Drive: DWORD; 
    Letter: Char; 
    ThumbnailCleaner: IEmptyVolumeCache; 
    Key: HKEY; 
    DisplayName: PWideChar; 
    Description: PWideChar; 
    Flags: DWORD; 
    ThumbnailCleanerCallBack: TThumbnailCleanerCallBack; 
begin 
    Drives := GetLogicalDrives; 
    Drive := 1; 
    for Letter := 'A' to 'Z' do 
    begin 
     if Drives and Drive <> 0 then 
     begin 
      OleCheck(CoCreateInstance(ThumbnailCleanerCLSID, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, ThumbnailCleaner)); 
      try 
      RegOpenKeyEx(HKEY_LOCAL_MACHINE, PWideChar('SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache'), 0, KEY_READ, Key); 
      try 
       DisplayName := nil; 
       Description := nil; 
       Flags := EVCF_SETTINGSMODE; 
       OleCheck(ThumbnailCleaner.Initialize(Key, PWideChar(Letter + ':\'), DisplayName, Description, Flags)); 
       try 
       if Assigned(DisplayName) then CoTaskMemFree(DisplayName); 
       if Assigned(Description) then CoTaskMemFree(Description); 
       ThumbnailCleanerCallBack := TThumbnailCleanerCallBack.Create; 
       OleCheck(ThumbnailCleaner.Purge(UInt64(-1), ThumbnailCleanerCallBack)); 
       finally 
       Flags := 0; 
       OleCheck(ThumbnailCleaner.Deactivate(Flags)); 
       end; 
      finally 
       RegCloseKey(Key); 
      end; 
      finally 
      ThumbnailCleaner := nil; 
      end; 
     end; 
     Drive := Drive shl 1; 
    end; 
end; 
+0

謝謝!我現在正在查看IEmptyVolumeCache –

相關問題