using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices; //命名空間
using System.Reflection; //提供加載類型 Pointer指針
using Microsoft.Win32; //RegistryKeynamespace GetIE
{#region COM接口實現獲取IE歷史記錄//自定義結構 IUrlHistorypublic struct STATURL{public static uint SIZEOF_STATURL =(uint)Marshal.SizeOf(typeof(STATURL));public uint cbSize; //網頁大小[MarshalAs(UnmanagedType.LPWStr)] //網頁Urlpublic string pwcsUrl;[MarshalAs(UnmanagedType.LPWStr)] //網頁標題public string pwcsTitle;public System.Runtime.InteropServices.ComTypes.FILETIMEftLastVisited, //網頁最近訪問時間ftLastUpdated, //網頁最近更新時間ftExpires;public uint dwFlags;}//ComImport屬性通過guid調用com組件[ComImport, Guid("3C374A42-BAE4-11CF-BF7D-00AA006946EE"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface IEnumSTATURL{[PreserveSig]//搜索IE歷史記錄匹配的搜索模式并復制到指定緩沖區uint Next(uint celt, out STATURL rgelt, out uint pceltFetched);void Skip(uint celt);void Reset();void Clone(out IEnumSTATURL ppenum);void SetFilter([MarshalAs(UnmanagedType.LPWStr)] string poszFilter,uint dwFlags);}[ComImport, Guid("AFA0DC11-C313-11d0-831A-00C04FD5AE38"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface IUrlHistoryStg2{#region IUrlHistoryStg methodsvoid AddUrl([MarshalAs(UnmanagedType.LPWStr)] string pocsUrl,[MarshalAs(UnmanagedType.LPWStr)] string pocsTitle,uint dwFlags);void DeleteUrl([MarshalAs(UnmanagedType.LPWStr)] string pocsUrl,uint dwFlags);void QueryUrl([MarshalAs(UnmanagedType.LPWStr)] string pocsUrl,uint dwFlags,ref STATURL lpSTATURL);void BindToObject([MarshalAs(UnmanagedType.LPWStr)] string pocsUrl,ref Guid riid,[MarshalAs(UnmanagedType.IUnknown)] out object ppvOut);IEnumSTATURL EnumUrls();#endregionvoid AddUrlAndNotify([MarshalAs(UnmanagedType.LPWStr)] string pocsUrl,[MarshalAs(UnmanagedType.LPWStr)] string pocsTitle,uint dwFlags,[MarshalAs(UnmanagedType.Bool)] bool fWriteHistory,[MarshalAs(UnmanagedType.IUnknown)] object /*IOleCommandTarget*/poctNotify,[MarshalAs(UnmanagedType.IUnknown)] object punkISFolder);void ClearHistory(); //清除歷史記錄}[ComImport, Guid("3C374A40-BAE4-11CF-BF7D-00AA006946EE")]class UrlHistory /* : IUrlHistoryStg[2] */ { }#endregion//調用COM接口IUrHistory方法實現public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){IUrlHistoryStg2 vUrlHistoryStg2 = (IUrlHistoryStg2)new UrlHistory();IEnumSTATURL vEnumSTATURL = vUrlHistoryStg2.EnumUrls();STATURL vSTATURL;uint vFectched;while (vEnumSTATURL.Next(1, out vSTATURL, out vFectched) == 0){ richTextBox1.AppendText(string.Format("{0}\r\n{1}\r\n",vSTATURL.pwcsTitle, vSTATURL.pwcsUrl));}}}
}
經過我的處理后運行結果如下圖所示: ?
這里也提供他的一篇文章,采用另外一種方法調用IE的API函數實現 http://blog.sina.com.cn/s/blog_589d32f5010007xf.html 但是我也遇到了一個問題,就是它的ftLastVisited(The last time the user visited this page)存儲該網頁最后訪問時間,想通過該時間進行獲取今天訪問的或排序輸出前100.但是獲取該時間時總輸出錯誤,同時將FILETIME轉換成SYSTEMTIME或time_t都沒成功.希望以后能解決. 最后該文章主要是結合自己的實際東西講解,如果你剛好遇到類似的問題就可能對你有所幫助.同時如果在文章中遇到錯誤或不足的地方,請海涵!最重要的是感謝上面提到的博主.希望能把獲取時間等問題也解決.請尊重作者的勞動果實,勿噴!!! (By:Eastmount 2014-4-3 夜2點半 原創CSDNhttp://blog.csdn.net/eastmount/)