- usingSystem;
- usingSystem.Runtime.InteropServices;
- usingSystem.IO;
- namespaceWinAssistant
- {
- //Classfordeletingtherubbish.
- classDelRubbish
- {
- #region清空临时InternetCache
- //ForPInvoke:ContainsinformationaboutanentryintheInternetcache
- [StructLayout(LayoutKind.Explicit,Size=80)]
- publicstructINTERNET_CACHE_ENTRY_INFOA
- {
- [FieldOffset(0)]
- publicuintdwStructSize;
- [FieldOffset(4)]
- publicIntPtrlpszSourceUrlName;
- [FieldOffset(8)]
- publicIntPtrlpszLocalFileName;
- [FieldOffset(12)]
- publicuintCacheEntryType;
- [FieldOffset(16)]
- publicuintdwUseCount;
- [FieldOffset(20)]
- publicuintdwHitRate;
- [FieldOffset(24)]
- publicuintdwSizeLow;
- [FieldOffset(28)]
- publicuintdwSizeHigh;
- [FieldOffset(32)]
- publicFILETIMELastModifiedTime;
- [FieldOffset(40)]
- publicFILETIMEExpireTime;
- [FieldOffset(48)]
- publicFILETIMELastAccessTime;
- [FieldOffset(56)]
- publicFILETIMELastSyncTime;
- [FieldOffset(64)]
- publicIntPtrlpHeaderInfo;
- [FieldOffset(68)]
- publicuintdwHeaderInfoSize;
- [FieldOffset(72)]
- publicIntPtrlpszFileExtension;
- [FieldOffset(76)]
- publicuintdwReserved;
- [FieldOffset(76)]
- publicuintdwExemptDelta;
- }
- #regionDllimport
- //ForPInvoke:InitiatestheenumerationofthecachegroupsintheInternetcache
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="FindFirstUrlCacheGroup",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternIntPtrFindFirstUrlCacheGroup(
- intdwFlags,
- intdwFilter,
- IntPtrlpSearchCondition,
- intdwSearchCondition,
- reflonglpGroupId,
- IntPtrlpReserved);
- //ForPInvoke:Retrievesthenextcachegroupinacachegroupenumeration
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="FindNextUrlCacheGroup",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternboolFindNextUrlCacheGroup(
- IntPtrhFind,
- reflonglpGroupId,
- IntPtrlpReserved);
- //ForPInvoke:ReleasesthespecifiedGROUPIDandanyassociatedstateinthecacheindexfile
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="DeleteUrlCacheGroup",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternboolDeleteUrlCacheGroup(
- longGroupId,
- intdwFlags,
- IntPtrlpReserved);
- //ForPInvoke:BeginstheenumerationoftheInternetcache
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="FindFirstUrlCacheEntryA",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternIntPtrFindFirstUrlCacheEntry(
- [MarshalAs(UnmanagedType.LPTStr)]stringlpszUrlSearchPattern,
- IntPtrlpFirstCacheEntryInfo,
- refintlpdwFirstCacheEntryInfoBufferSize);
- //ForPInvoke:RetrievesthenextentryintheInternetcache
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="FindNextUrlCacheEntryA",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternboolFindNextUrlCacheEntry(
- IntPtrhFind,
- IntPtrlpNextCacheEntryInfo,
- refintlpdwNextCacheEntryInfoBufferSize);
- //ForPInvoke:Removesthefilethatisassociatedwiththesourcenamefromthecache,ifthefileexists
- [DllImport(@"wininet",
- SetLastError=true,
- CharSet=CharSet.Auto,
- EntryPoint="DeleteUrlCacheEntryA",
- CallingConvention=CallingConvention.StdCall)]
- privatestaticexternboolDeleteUrlCacheEntry(
- IntPtrlpszUrlName);
- #endregionDllimport
- #regionmethods
- publicstaticvoidDeleteNetCache()
- {
- #regionvariables
- //Indicatesthatallofthecachegroupsintheuser'ssystemshouldbeenumerated
- constintCACHEGROUP_SEARCH_ALL=0x0;
- //Indicatesthatallthecacheentriesthatareassociatedwiththecachegroup
- //shouldbedeleted,unlesstheentrybelongstoanothercachegroup.
- constintCACHEGROUP_FLAG_FLUSHURL_ONDELETE=0x2;
- //Filenotfound.
- constintERROR_FILE_NOT_FOUND=0x2;
- //Nomoreitemshavebeenfound.
- constintERROR_NO_MORE_ITEMS=259;
- //PointertoaGROUPIDvariable
- longgroupId=0;
- //Localvariables
- intcacheEntryInfoBufferSizeInitial=0;
- intcacheEntryInfoBufferSize=0;
- IntPtrcacheEntryInfoBuffer=IntPtr.Zero;
- INTERNET_CACHE_ENTRY_INFOAinternetCacheEntry;
- IntPtrenumHandle=IntPtr.Zero;
- boolreturnValue=false;
- #endregionvariables
- //Deletethegroupsfirst.
- //Groupsmaynotalwaysexistonthesystem.
- //Formoreinformation,visitthefollowingMicrosoftWebsite:
- //http://msdn.microsoft.com/library/?url=/workshop/networking/wininet/overview/cache.asp
- //Bydefault,aURLdoesnotbelongtoanygroup.Therefore,thatcachemaybecome
- //emptyevenwhentheCacheGroupAPIsarenotusedbecausetheexistingURLdoesnotbelongtoanygroup.
- enumHandle=FindFirstUrlCacheGroup(0,CACHEGROUP_SEARCH_ALL,IntPtr.Zero,0,refgroupId,IntPtr.Zero);
- //IftherearenoitemsintheCache,youarefinished.
- if(enumHandle!=IntPtr.Zero&&ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error())
- return;
- //LoopthroughCacheGroup,andthendeleteentries.
- while(true)
- {
- //DeleteaparticularCacheGroup.
- returnValue=DeleteUrlCacheGroup(groupId,CACHEGROUP_FLAG_FLUSHURL_ONDELETE,IntPtr.Zero);
- if(!returnValue&&ERROR_FILE_NOT_FOUND==Marshal.GetLastWin32Error())
- {
- returnValue=FindNextUrlCacheGroup(enumHandle,refgroupId,IntPtr.Zero);
- }
- if(!returnValue&&(ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error()||ERROR_FILE_NOT_FOUND==Marshal.GetLastWin32Error()))
- break;
- }
- //StarttodeleteURLsthatdonotbelongtoanygroup.
- enumHandle=FindFirstUrlCacheEntry(null,IntPtr.Zero,refcacheEntryInfoBufferSizeInitial);
- if(enumHandle!=IntPtr.Zero&&ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error())
- return;
- cacheEntryInfoBufferSize=cacheEntryInfoBufferSizeInitial;
- cacheEntryInfoBuffer=Marshal.AllocHGlobal(cacheEntryInfoBufferSize);
- enumHandle=FindFirstUrlCacheEntry(null,cacheEntryInfoBuffer,refcacheEntryInfoBufferSizeInitial);
- while(true)
- {
- internetCacheEntry=(INTERNET_CACHE_ENTRY_INFOA)Marshal.PtrToStructure(cacheEntryInfoBuffer,typeof(INTERNET_CACHE_ENTRY_INFOA));
- cacheEntryInfoBufferSizeInitial=cacheEntryInfoBufferSize;
- returnValue=DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName);
- if(!returnValue)
- {
- returnValue=FindNextUrlCacheEntry(enumHandle,cacheEntryInfoBuffer,refcacheEntryInfoBufferSizeInitial);
- }
- if(!returnValue&&ERROR_NO_MORE_ITEMS==Marshal.GetLastWin32Error())
- {
- break;
- }
- if(!returnValue&&cacheEntryInfoBufferSizeInitial>cacheEntryInfoBufferSize)
- {
- cacheEntryInfoBufferSize=cacheEntryInfoBufferSizeInitial;
- cacheEntryInfoBuffer=Marshal.ReAllocHGlobal(cacheEntryInfoBuffer,(IntPtr)cacheEntryInfoBufferSize);
- returnValue=FindNextUrlCacheEntry(enumHandle,cacheEntryInfoBuffer,refcacheEntryInfoBufferSizeInitial);
- }
- }
- Marshal.FreeHGlobal(cacheEntryInfoBuffer);
- }
- #endregionmethods
- #endregion清空临时Cache
- #region清空回收站
- [Flags()]
- publicenumSHERB
- {
- SHERB_NOCONFIRMATION=0x00000001,
- SHERB_NOPROGRESSUI=0x00000002,
- SHERB_NOSOUND=0x00000004
- }
- [DllImport("shell32.dll",CharSet=CharSet.Auto)]
- privatestaticexternuintSHEmptyRecycleBin(
- IntPtrhwnd,
- stringpszRootPath,
- SHERBdwFlags);
- publicstaticvoidDeleteRecycle()
- {
- SHEmptyRecycleBin(IntPtr.Zero,null,SHERB.SHERB_NOCONFIRMATION);
- }
- #endregion清空回收站
- #region清空recent、temp文件夹
- publicstaticvoidDeleteFile()
- {
- stringtemp=Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData).ToString().Replace("ApplicationData",null)+"Temp";
- stringrecent=Environment.GetFolderPath(Environment.SpecialFolder.Recent).ToString();
- CleanUp(temp);
- CleanUp(recent);
- }
- privatestaticvoidCleanUp(stringdir)
- {
- foreach(stringdinDirectory.GetFileSystemEntries(dir))
- {
- try
- {
- if(File.Exists(d))
- {
- File.Delete(d);
- }
- else
- {
- //CleanUp(d);
- Directory.Delete(d,true);
- }
- }
- catch{}
- }
- }
- #endregion清空recent、temp文件夹
- }
- }
c# 一个删除垃圾文件处理的类
最新推荐文章于 2018-03-21 11:08:46 发布
本文介绍了一种用于清除Windows系统中Internet缓存、回收站及recent与temp文件夹内临时文件的方法。通过调用WinAPI,实现对指定文件与缓存的有效删除,有助于释放磁盘空间并提高系统运行效率。
1314

被折叠的 条评论
为什么被折叠?



