清除webBrowser 缓存和Cookie的解决方案

时间:2022-08-29 14:10:47

清除webBrowser 缓存和Cookie的解决方案

 

通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除

解决方案1:

清除webBrowser 缓存和Cookie的解决方案
public enum ShowCommands : int

{

SW_HIDE = 0,

SW_SHOWNORMAL = 1,

SW_NORMAL = 1,

SW_SHOWMINIMIZED = 2,

SW_SHOWMAXIMIZED = 3,

SW_MAXIMIZE = 3,

SW_SHOWNOACTIVATE = 4,

SW_SHOW = 5,

SW_MINIMIZE = 6,

SW_SHOWMINNOACTIVE = 7,

SW_SHOWNA = 8,

SW_RESTORE = 9,

SW_SHOWDEFAULT = 10,

SW_FORCEMINIMIZE = 11,

SW_MAX = 11

}
清除webBrowser 缓存和Cookie的解决方案

 

[DllImport("shell32.dll")]

static extern IntPtr ShellExecute( IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);

 

//清除IE临时文件

                ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE);

 

 

其中ClearMyTracksByProcess 可进行选择设置 :

 

Temporary Internet Files  (Internet临时文件)

 

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8

 

Cookies

 

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2

 

History (历史记录)

 

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1

 

 

 Form. Data (表单数据)

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16

 

 

 Passwords (密码)

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32

 

 

 Delete All  (全部删除)

RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255

 

解决方案2:快速清除webBrowser Cookie

 

wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count - 1))

 

 

另外一个

 

 

  1.  
    [ DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
  2.  
    public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
  3.  
    private unsafe void SuppressWininetBehavior()
  4.  
    {
  5.  
    /* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
  6.  
    * INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
  7.  
    * A general purpose option that is used to suppress behaviors on a process-wide basis.
  8.  
    * The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress.
  9.  
    * This option cannot be queried with InternetQueryOption.
  10.  
    *
  11.  
    * INTERNET_SUPPRESS_COOKIE_PERSIST (3):
  12.  
    * Suppresses the persistence of cookies, even if the server has specified them as persistent.
  13.  
    * Version: Requires Internet Explorer 8.0 or later.
  14.  
    */
  15.  
    int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
  16.  
    int* optionPtr = &option;
  17.  
    bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
  18.  
    if (!success)
  19.  
    {
  20.  
    MessageBox.Show( "Something went wrong !>?");
  21.  
    }
  22.  
    }
  23.