ローカルファイルの履歴を削除する。
Win32APIやNETはホントよくわかりません。見よう見まねです。苦労しました。
DeleteHist.vb
Imports System.Runtime.InteropServices
'Class for deleting the cache.
Public Class Class1
'For PInvoke: Contains information about an entry in the Internet cache
<StructLayout(LayoutKind.Explicit, Size:=80)> _
Public Structure INTERNET_CACHE_ENTRY_INFOA
<FieldOffset(0)> Public dwStructSize As UInt32
<FieldOffset(4)> Public lpszSourceUrlName As String
<FieldOffset(8)> Public lpszLocalFileName As String
<FieldOffset(12)> Public CacheEntryType As UInt32
<FieldOffset(16)> Public dwUseCount As UInt32
<FieldOffset(20)> Public dwHitRate As UInt32
<FieldOffset(24)> Public dwSizeLow As UInt32
<FieldOffset(28)> Public dwSizeHigh As UInt32
<FieldOffset(32)> Public LastModifiedTime As Long
<FieldOffset(40)> Public ExpireTime As Long
<FieldOffset(48)> Public LastAccessTime As Long
<FieldOffset(56)> Public LastSyncTime As Long
<FieldOffset(64)> Public lpHeaderInfo As IntPtr
<FieldOffset(68)> Public dwHeaderInfoSize As UInt32
<FieldOffset(72)> Public lpszFileExtension As IntPtr
<FieldOffset(76)> Public dwReserved As UInt32
<FieldOffset(76)> Public dwExemptDelta As UInt32
End Structure
'For PInvoke: Begins the enumeration of the Internet cache
<DllImport("wininet.dll", _
SetLastError:=True, _
CharSet:=CharSet.Auto, _
EntryPoint:="FindFirstUrlCacheEntryA", _
CallingConvention:=CallingConvention.StdCall)> _
Shared Function FindFirstUrlCacheEntry( _
<MarshalAs(UnmanagedType.LPStr)> ByVal lpszUrlSearchPattern As String, _
ByVal lpFirstCacheEntryInfo As IntPtr, _
ByRef lpdwFirstCacheEntryInfoBufferSize As Int32) As IntPtr
End Function
'For PInvoke: Retrieves the next entry in the Internet cache
<DllImport("wininet.dll", _
SetLastError:=True, _
CharSet:=CharSet.Auto, _
EntryPoint:="FindNextUrlCacheEntryA", _
CallingConvention:=CallingConvention.StdCall)> _
Shared Function FindNextUrlCacheEntry( _
ByVal hFind As IntPtr, _
ByVal lpNextCacheEntryInfo As IntPtr, _
ByRef lpdwNextCacheEntryInfoBufferSize As Integer) As Boolean
End Function
'For PInvoke: Removes the file that is associated with the source name from the cache, if the file exists
<DllImport("wininet.dll", _
SetLastError:=True, _
CharSet:=CharSet.Auto, _
EntryPoint:="DeleteUrlCacheEntryW", _
CallingConvention:=CallingConvention.StdCall)> _
Shared Function DeleteUrlCacheEntry( _
ByVal lpszUrlName As String) As Boolean
End Function
End Class
'次のコードを Module1.vb モジュールに追加:
'Imports System.Runtime.InteropServices
Module Module1
Sub Main()
'No more items have been found.
'File not found.
Const ERROR_FILE_NOT_FOUND As Integer = &H2
Const ERROR_NO_MORE_ITEMS As Integer = 259
Const NORMAL_CACHE_ENTRY As Integer = 1
Const URLHISTORY_CACHE_ENTRY As Integer = &H200000
'Local variables
Dim cacheEntryInfoBufferSizeInitial As Integer = 0
Dim cacheEntryInfoBufferSize As Integer = 0
Dim cacheEntryInfoBuffer As IntPtr = IntPtr.Zero
Dim internetCacheEntry As Class1.INTERNET_CACHE_ENTRY_INFOA
Dim enumHandle As IntPtr = IntPtr.Zero
Dim returnValue As Boolean = False
Dim LastVisited As DateTime
enumHandle = Class1.FindFirstUrlCacheEntry("Visited:", IntPtr.Zero, cacheEntryInfoBufferSizeInitial)
If (enumHandle.Equals(IntPtr.Zero) And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error())) Then
Exit Sub
End If
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
cacheEntryInfoBuffer = Marshal.AllocHGlobal(cacheEntryInfoBufferSize)
enumHandle = Class1.FindFirstUrlCacheEntry("Visited:", cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
Console.WriteLine("""LastVisited"",""URL""")
While (True)
internetCacheEntry = CType(Marshal.PtrToStructure(cacheEntryInfoBuffer, GetType(Class1.INTERNET_CACHE_ENTRY_INFOA)), Class1.INTERNET_CACHE_ENTRY_INFOA)
cacheEntryInfoBufferSizeInitial = cacheEntryInfoBufferSize
If (internetCacheEntry.CacheEntryType = URLHISTORY_CACHE_ENTRY + NORMAL_CACHE_ENTRY) Then
If (internetCacheEntry.lpszSourceUrlName.Contains("file://")) Then
LastVisited=DateTime.FromFileTime(internetCacheEntry.LastAccessTime)
Console.WriteLine("""{1}"",""{0}""",internetCacheEntry.lpszSourceUrlName,LastVisited)
returnValue = Class1.DeleteUrlCacheEntry(internetCacheEntry.lpszSourceUrlName)
If (Not returnValue AndAlso Not ERROR_FILE_NOT_FOUND.Equals(Marshal.GetLastWin32Error())) Then
Console.WriteLine("Error Deleting: {0}", Marshal.GetLastWin32Error())
End If
End If
End If
returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
If (Not returnValue And ERROR_NO_MORE_ITEMS.Equals(Marshal.GetLastWin32Error())) Then
Exit While
End If
If (Not returnValue And cacheEntryInfoBufferSizeInitial > cacheEntryInfoBufferSize) Then
cacheEntryInfoBufferSize = cacheEntryInfoBufferSizeInitial
Dim tempIntPtr As New IntPtr(cacheEntryInfoBufferSize)
cacheEntryInfoBuffer = Marshal.ReAllocHGlobal(cacheEntryInfoBuffer, tempIntPtr)
returnValue = Class1.FindNextUrlCacheEntry(enumHandle, cacheEntryInfoBuffer, cacheEntryInfoBufferSizeInitial)
End If
End While
Marshal.FreeHGlobal(cacheEntryInfoBuffer)
End Sub
End Module