Om cookies uit te lezen die door internet explorer word opgeslagen kun je onderstaande code gebruiken.
De gegevens worden in kolommen gezet en onder verdeeld in 8 kolommen namelijk.: SourceUrlName, LocalFileName, HitRate, Size, LastModifiedTime, ExpireTime, LastAccessTime en LastSyncTime.
Maak een nieuw project aan en dubbel klik op je form.
Verwijder hier alle codes, dus.:
Visual Basic Code:
Nu bovenstaande code weg is gehaald plaak je onderstaande code er voor in de plaatst.:
Visual Basic Code:
Voeg nu een CLASS aan je project toe met de naam EntryInfo
Plak hier onderstaande code erin.:
Visual Basic Code:
Als je nu het project gaat testen zie je netjes een tabel met 8 kolommen en de gevonden cookie gegevens.
Om nu van zoeen cookie de gegevens te zien kun je op de cookie dubbel klikken.
Let wel even op , deze code werkt alleen voor de cookies die zijn opgeslagen door Internet Explorer, andere Internet programma's hebben hun eigen manier van opslaan.
De gegevens worden in kolommen gezet en onder verdeeld in 8 kolommen namelijk.: SourceUrlName, LocalFileName, HitRate, Size, LastModifiedTime, ExpireTime, LastAccessTime en LastSyncTime.
Maak een nieuw project aan en dubbel klik op je form.
Verwijder hier alle codes, dus.:
Visual Basic Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load End Sub End Class
Nu bovenstaande code weg is gehaald plaak je onderstaande code er voor in de plaatst.:
Visual Basic Code:
Option Strict On Option Explicit On Public Class Form1 Private WithEvents lvCookies As New ListView Sub New() InitializeComponent() Me.Size = New Size(1024, 480) lvCookies.Bounds = New Rectangle(5, 5, Me.ClientSize.Width - 10, Me.ClientSize.Height - 10) lvCookies.Columns.Add("SourceUrlName") lvCookies.Columns.Add("LocalFileName") ' lvCookies.Columns.Add("UseCount") lvCookies.Columns.Add("HitRate") lvCookies.Columns.Add("Size") lvCookies.Columns.Add("LastModifiedTime") lvCookies.Columns.Add("ExpireTime") lvCookies.Columns.Add("LastAccessTime") lvCookies.Columns.Add("LastSyncTime") 'lvCookies.Columns.Add("HeaderInfo") 'lvCookies.Columns.Add("FileExtension") 'lvCookies.Columns.Add("ExemptDelta") lvCookies.View = View.Details lvCookies.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom Me.Controls.Add(lvCookies) End Sub Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim cookies As IList(Of EntryInfo) = EntryInfo.GetCookies For Each cookie In cookies Dim lvi As New ListViewItem(cookie.SourceUrlName) lvi.SubItems.Add(cookie.LocalFileName) ' lvi.SubItems.Add(cookie.UseCount.ToString) lvi.SubItems.Add(cookie.HitRate.ToString) lvi.SubItems.Add(cookie.Size.ToString) lvi.SubItems.Add(cookie.LastModifiedTime.ToShortDateString) lvi.SubItems.Add(cookie.ExpireTime.ToShortDateString) lvi.SubItems.Add(cookie.LastAccessTime.ToShortDateString) lvi.SubItems.Add(cookie.LastSyncTime.ToShortDateString) 'lvi.SubItems.Add(cookie.HeaderInfo) 'lvi.SubItems.Add(cookie.FileExtension) 'lvi.SubItems.Add(cookie.ExemptDelta.ToString) lvi.Tag = cookie lvCookies.Items.Add(lvi) Next SetWidths() End Sub Private Sub lvCookies_DoubleClick(sender As Object, e As System.EventArgs) Handles lvCookies.DoubleClick If lvCookies.SelectedItems(0) IsNot Nothing Then Dim cookie As EntryInfo = DirectCast(lvCookies.SelectedItems(0).Tag, EntryInfo) Dim cookieText As String = My.Computer.FileSystem.ReadAllText(cookie.LocalFileName) MessageBox.Show(cookieText, "Cookie text:") End If End Sub Private Sub SetWidths() For i As Integer = 0 To lvCookies.Columns.Count - 1 lvCookies.Columns(i).AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize) Dim w As Integer = lvCookies.Columns(i).Width lvCookies.Columns(i).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent) If lvCookies.Columns(i).Width < w Then lvCookies.Columns(i).Width = w Next End Sub End Class
Voeg nu een CLASS aan je project toe met de naam EntryInfo
Plak hier onderstaande code erin.:
Visual Basic Code:
Imports System.Runtime.InteropServices Imports System.Security Imports System.ComponentModel Public Class EntryInfo Private Const NoMoreData As Integer = 259 Private Const BufferTooSmall As Integer = 122 <Flags()> _ Private Enum Filters NORMAL_CACHE_ENTRY = 1 STICKY_CACHE_ENTRY = 4 EDITED_CACHE_ENTRY = 8 TRACK_OFFLINE_CACHE_ENTRY = &H10 TRACK_ONLINE_CACHE_ENTRY = &H20 SPARSE_CACHE_ENTRY = &H10000 COOKIE_CACHE_ENTRY = &H100000 URLHISTORY_CACHE_ENTRY = &H200000 End Enum <SuppressUnmanagedCodeSecurity()> _ Private Class NativeMethods <DllImport("wininet.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _ Public Shared Function FindFirstUrlCacheEntry( _ searchPattern As String, _ ByVal firstCacheEntryInfo As IntPtr, _ ByRef cacheEntryInfoSize As Integer) As IntPtr End Function <DllImport("wininet.dll", CharSet:=CharSet.Unicode, SetLastError:=True)> _ Public Shared Function FindNextUrlCacheEntry( _ hEnumHandle As IntPtr, _ ByVal nextCacheEntryInfo As IntPtr, _ ByRef cacheEntryInfoSize As Integer) As <MarshalAs(UnmanagedType.Bool)> Boolean End Function End Class <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _ Private Structure InternetCacheEntryInfo Public StructSize As Integer Public SourceUrlName As String Public LocalFileName As String Public CacheEntryType As Integer Public UseCount As Integer Public HitRate As Integer Public Size As Long Public LastModifiedTime As Long Public ExpireTime As Long Public LastAccessTime As Long Public LastSyncTime As Long Public HeaderInfo As IntPtr Public HeaderInfoSize As Integer Public FileExtension As String Public Union As Integer ' not interested enough in the union to declare it. ' Then the header data... End Structure ' .Net 4.0 property syntax. Covert them to long hand if you are using an earlier version. Public Property SourceUrlName As String Public Property LocalFileName As String ' Public Property UseCount As Integer Public Property HitRate As Integer Public Property Size As Long Public Property LastModifiedTime As DateTime Public Property ExpireTime As DateTime Public Property LastAccessTime As DateTime Public Property LastSyncTime As DateTime 'Public Property HeaderInfo As String 'Public Property FileExtension As String 'Public Property ExemptDelta As Integer ''' <summary> ''' Gets a managed object representing the information from the IntPtr. ''' </summary> Private Shared Function FromIntPtr(pInfo As IntPtr) As EntryInfo Dim nativeStructure As InternetCacheEntryInfo = DirectCast(Marshal.PtrToStructure(pInfo, GetType(InternetCacheEntryInfo)), InternetCacheEntryInfo) Dim managedClass As New EntryInfo managedClass.SourceUrlName = nativeStructure.SourceUrlName managedClass.LocalFileName = nativeStructure.LocalFileName ' managedClass.UseCount = nativeStructure.UseCount managedClass.HitRate = nativeStructure.HitRate managedClass.Size = nativeStructure.Size managedClass.LastModifiedTime = DateTime.FromFileTime(nativeStructure.LastModifiedTime) managedClass.ExpireTime = DateTime.FromFileTime(nativeStructure.ExpireTime) managedClass.LastAccessTime = DateTime.FromFileTime(nativeStructure.LastAccessTime) managedClass.LastSyncTime = DateTime.FromFileTime(nativeStructure.LastSyncTime) 'managedClass.HeaderInfo = Marshal.PtrToStringUni(nativeStructure.HeaderInfo) 'managedClass.FileExtension = nativeStructure.FileExtension 'managedClass.ExemptDelta = nativeStructure.Union Return managedClass End Function Public Shared Function GetCookies() As IList(Of EntryInfo) Dim infos As New List(Of EntryInfo) Dim enumHandle As IntPtr Dim firstInfo As EntryInfo = GetFirst(enumHandle) If firstInfo Is Nothing Then Return infos ' got none. infos.Add(firstInfo) While enumHandle <> IntPtr.Zero Dim cookie As EntryInfo = GetNext(enumHandle) If enumHandle <> IntPtr.Zero Then infos.Add(cookie) End While Return infos End Function Private Shared Function GetFirst(ByRef enumHandle As IntPtr) As EntryInfo Dim info As New IntPtr Dim infoSize As Integer = 0 ' FirstCall: How much memory is required enumHandle = NativeMethods.FindFirstUrlCacheEntry("cookie:", info, infoSize) Dim err As Integer = Marshal.GetLastWin32Error If err <> BufferTooSmall Then Throw New Win32Exception(err) ' Second call: With a blob of memory Try info = Marshal.AllocHGlobal(infoSize) enumHandle = NativeMethods.FindFirstUrlCacheEntry("cookie:", info, infoSize) Return EntryInfo.FromIntPtr(info) Finally Marshal.FreeHGlobal(info) End Try Return Nothing End Function Private Shared Function GetNext(ByRef enumHandle As IntPtr) As EntryInfo Dim info As New IntPtr Dim infoSize As Integer = 0 Dim result As Boolean = NativeMethods.FindNextUrlCacheEntry(enumHandle, info, infoSize) Dim err As Integer = Marshal.GetLastWin32Error If err = NoMoreData Then ' No more data. We set enumhandle to 0 to let us know we've finised, and return nothing. enumHandle = IntPtr.Zero Return Nothing End If If err <> BufferTooSmall Then Throw New Win32Exception(err) Try info = Marshal.AllocHGlobal(infoSize) result = NativeMethods.FindNextUrlCacheEntry(enumHandle, info, infoSize) If result Then Return EntryInfo.FromIntPtr(info) Finally Marshal.FreeHGlobal(info) End Try enumHandle = IntPtr.Zero Return Nothing End Function End Class
Als je nu het project gaat testen zie je netjes een tabel met 8 kolommen en de gevonden cookie gegevens.
Om nu van zoeen cookie de gegevens te zien kun je op de cookie dubbel klikken.

Let wel even op , deze code werkt alleen voor de cookies die zijn opgeslagen door Internet Explorer, andere Internet programma's hebben hun eigen manier van opslaan.