gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:32 25 4
gpt4 key购买 nike

我正在编写一个为 Windows 文件夹创建缩略图图标的程序。我想为用户提供一种刷新缩略图缓存的简单方法。

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

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

最佳答案

您可以为 Window 缩略图清理器扩展创建一个主机,它将模拟 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;

关于c# - 以编程方式清理 Windows 缩略图缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23677175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com