有关IE的操作(收藏夹,清理缓存等)

1.添加网页到收藏夹的函数:
procedure AddURL(Folder, Url, Title: string);
var
  MyUrlFile: TextFile;
begin
  if Folder[Length(Folder)] <> '\' then Folder := Folder + '\';
  if not DirectoryExists(Folder) then ForceDirectories(Folder);
  try
    AssignFile(MyUrlFile, Folder + title + '.url');
    Rewrite(MyUrlFile);
    WriteLn(MyUrlFile, '[InternetShortcut]');
    WriteLn(MyUrlFile, 'URL=' + url);
  finally
    Closefile(MyUrlFile);
  end;
end;
View Code

 2、清理IE缓存的代码(亲测可用)

 ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 8',nil,1); //IE缓存

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 2',nil,1); //IE cookies

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 1',nil,1); //IE history

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 16',nil,1); //form

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 32',nil,1); //auto save pass

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 255',nil,1); //all

ShellExecute(0,'open','rundll32.exe','inetcpl.cpl,ClearMyTracksByProcess 4351',nil,1); //all & addons
View Code

3、清理IE缓存的类 (好像清理不了 )

unit uCleanCookie;

interface
uses
  Registry, ShellApi, WinInet, ShlObj, ComObj, Windows, Forms, SysUtils, shdocvw;

type
  TIECookie = class
  private


  public
    procedure DelRegCache;
    function GetCookiesFolder: string;
    procedure DelCookie;
    procedure DelHistory;
      function ClearIEHistory:integer;
  published

  end;
  type
   TSTATURL    =    record
       cbSize:    DWORD;
       pwcsUrl:    DWORD;
       pwcsTitle:    DWORD;
       ftLastVisited:    FILETIME;
       ftLastUpdated:    FILETIME;
       ftExpires:    FILETIME;
       dwFlags:    DWORD;
   end;

type
   IEnumSTATURL    =    interface(IUnknown)
       ['{3C374A42-BAE4-11CF-BF7D-00AA006946EE}']
       function    Next(celt:    Integer;    out    elt;    pceltFetched:    PLongint):    HRESULT;    stdcall;
       function    Skip(celt:    Longint):    HRESULT;    stdcall;
       function    Reset:    HResult;    stdcall;
       function    Clone(out    ppenum:    IEnumSTATURL):    HResult;    stdcall;
       function    SetFilter(poszFilter:    PWideChar;    dwFlags:    DWORD):    HResult;    stdcall;
   end;

type
   IUrlHistoryStg    =    interface(IUnknown)
       ['{3C374A41-BAE4-11CF-BF7D-00AA006946EE}']
       function    AddUrl(pocsUrl:    PWideChar;    pocsTitle:    PWideChar;    dwFlags:    Integer):    HResult;    stdcall;
       function    DeleteUrl(pocsUrl:    PWideChar;    dwFlags:    Integer):    HResult;    stdcall;
       function    QueryUrl(pocsUrl:    PWideChar;    dwFlags:    Integer;    var    lpSTATURL:    TSTATURL):    HResult;    stdcall;
       function    BindToObject(pocsUrl:    PWideChar;    var    riid:    TGUID;    out    ppvOut:    Pointer):    HResult;    stdcall;
       function    EnumUrls(out    ppenum:    IEnumSTATURL):    HResult;    stdcall;
   end;

type
   IUrlHistoryStg2    =    interface(IUrlHistoryStg)
       ['{AFA0DC11-C313-11D0-831A-00C04FD5AE38}']
       function    AddUrlAndNotify(pocsUrl:    PWideChar;    pocsTitle:    PWideChar;    dwFlags:    Integer;
           fWriteHistory:    Integer;    var    poctNotify:    Pointer;
           const    punkISFolder:    IUnknown):    HResult;    stdcall;
       function    ClearHistory:    HResult;    stdcall;
   end;
implementation
{ TIECookie }

function TIECookie.ClearIEHistory: integer;
const
       CLSID_CUrlHistory:    TGUID    =    '{3C374A40-BAE4-11CF-BF7D-00AA006946EE}';
var
   IEHistory:IUrlHistoryStg2;
begin
   IEHistory:=CreateComObject(CLSID_CUrlHistory)    as    IUrlHistoryStg2;
   IEHistory.ClearHistory;

end;

procedure TIECookie.DelCookie;
var
  dir: string;
begin
  try
    InternetSetOption(nil, INTERNET_OPTION_END_BROWSER_SESSION, nil, 0);
    dir := GetCookiesFolder;
//ShellDeleteFile(dir+'\*.txt'+#0); //网上很多代码这里没有加最后的#0,在xp下经测试会报错
  except
    abort;
  end;
end;

procedure TIECookie.DelHistory;
var
  lpEntryInfo: PInternetCacheEntryInfo;
  hCacheDir: LongWord;
  dwEntrySize, dwLastError: LongWord;
begin
  try
    dwEntrySize := 0;
    FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
    GetMem(lpEntryInfo, dwEntrySize);
    hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
    if hCacheDir <> 0 then
      DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
    FreeMem(lpEntryInfo);
    repeat
      dwEntrySize := 0;
      FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^),
        dwEntrySize);
      dwLastError := GetLastError();
      if dwLastError = ERROR_INSUFFICIENT_BUFFER then //如果成功
      begin
        GetMem(lpEntryInfo, dwEntrySize); {分配dwEntrySize字节的内存}
        if FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize) then
          DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
        FreeMem(lpEntryInfo);
      end;
    until (dwLastError = ERROR_NO_MORE_ITEMS);
  except
    abort;
  end;

end;

procedure TIECookie.DelRegCache;
var
  reg: TRegistry;
begin
  reg := Tregistry.create;
  reg.RootKey := HKEY_CURRENT_USER;
  reg.DeleteKey('Software\Microsoft\Internet Explorer\TypedURLs');
  reg.Free;

end;

function TIECookie.GetCookiesFolder: string;
var
  pidl: pItemIDList;
  buffer: array[0..255] of char;
begin
  SHGetSpecialFolderLocation(
    application.Handle, CSIDL_COOKIES, pidl);
  SHGetPathFromIDList(pidl, buffer);
  result := strpas(buffer);
end;

function ShellDeleteFile(sFileName: string): Boolean;
var
  FOS: TSHFileOpStruct;
begin
  FillChar(FOS, SizeOf(FOS), 0); {记录清零}
  with FOS do
  begin
    wFunc := FO_DELETE; //删除
    pFrom := PChar(sFileName);
    fFlags := FOF_NOCONFIRMATION;
  end;
  Result := (SHFileOperation(FOS) = 0);

end;

end.

调用方法:


procedure TForm2.FormCreate(Sender: TObject);

begin
TClean:=TIECookie.Create;
try
TClean.DelRegCache;//清理注册表
TClean.DelCookie; //删除cookies(ie缓冲文件夹下面cookies文件)
TClean.DelHistory; //删除历史记录(ie缓冲文件夹下所有文件)
//C:\Documents and Settings\用户名\Local Settings\Temporary Internet Files
TClean.ClearIEHistory; //补充删除网页历史
 TClean.Free;
except
abort;
end;
end;
View Code
原文地址:https://www.cnblogs.com/stroll/p/4646805.html