以编程方式清理Windows缩略图缓存

时间:2014-05-15 11:40:27

标签: c# c++ windows winapi

我正在编写一个为Windows文件夹创建缩略图图标的程序。我想让用户轻松刷新缩略图缓存。

是否可以通过任何Windows API执行此操作?理想情况下,在C#中,但更有可能通过包装一些C ++。

如果没有,是否有批准的方法来确定用户缩略图缓存的位置并手动删除它们。或者最好只是启动cleanmgr并让用户从那里开始使用它?

1 个答案:

答案 0 :(得分:1)

您可以为Window thumbnail cleaner扩展创建一个主机,它将模拟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;