194 lines
6.4 KiB
C#
194 lines
6.4 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using Microsoft.Win32;
|
|
|
|
namespace AllBase
|
|
{
|
|
/// <summary>
|
|
/// IE清理程序
|
|
/// </summary>
|
|
public static class IECookies
|
|
{
|
|
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData,
|
|
ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
|
|
|
|
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData,
|
|
int dwFlags, IntPtr dwReserved);
|
|
|
|
[DllImport("shell32.dll")]
|
|
static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, ShowCommands nShowCmd);
|
|
|
|
|
|
private const int INTERNET_COOKIE_HTTPONLY = 0x00002000;
|
|
|
|
|
|
[DllImport("wininet.dll", SetLastError = true)]
|
|
private static extern bool InternetGetCookieEx(
|
|
string url,
|
|
string cookieName,
|
|
StringBuilder cookieData,
|
|
ref int size,
|
|
int flags,
|
|
IntPtr pReserved);
|
|
/// <summary>
|
|
/// 获取当前webbrowser 下的cookies
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static string GetCookieByWebBrowser(string url)
|
|
{
|
|
int size = 512;
|
|
StringBuilder sb = new StringBuilder(size);
|
|
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
|
|
{
|
|
if (size < 0)
|
|
{
|
|
return null;
|
|
}
|
|
sb = new StringBuilder(size);
|
|
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
/// <summary>
|
|
/// 获取webbrowser UserAgent
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetUserAgent()
|
|
{
|
|
WebBrowser wb = new WebBrowser();
|
|
wb.Navigate("about:blank");
|
|
while (wb.IsBusy) Application.DoEvents();
|
|
object window = wb.Document.Window.DomWindow;
|
|
Type wt = window.GetType();
|
|
object navigator = wt.InvokeMember("navigator", BindingFlags.GetProperty, null, window, new object[] { });
|
|
Type nt = navigator.GetType();
|
|
object userAgent = nt.InvokeMember("userAgent", BindingFlags.GetProperty, null, navigator, new object[] { });
|
|
return userAgent.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用IE打开网页
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
public static void OpenUrlByIE(string url)
|
|
{
|
|
Process.Start("iexplore.exe", url);
|
|
}
|
|
/// <summary>
|
|
/// 关闭所有IE浏览器
|
|
/// </summary>
|
|
public static void CloseIE()
|
|
{
|
|
foreach (Process thisproc in Process.GetProcesses())
|
|
{
|
|
if (thisproc.ProcessName.ToLower() == "iexplore")
|
|
thisproc.Kill();
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取IE下的cookies
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static string GetCookies(string url)
|
|
{
|
|
uint datasize = 256;
|
|
StringBuilder cookieData = new StringBuilder((int)datasize);
|
|
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
|
|
{
|
|
if (datasize < 0)
|
|
return null;
|
|
cookieData = new StringBuilder((int)datasize);
|
|
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
|
|
return null;
|
|
}
|
|
return cookieData.ToString();
|
|
}
|
|
/// <summary>
|
|
/// 清除IE所有缓存
|
|
/// </summary>
|
|
public static void ClearAll()
|
|
{
|
|
ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 8", "", ShowCommands.SW_HIDE);
|
|
}
|
|
|
|
|
|
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
|
|
}
|
|
/// <summary>
|
|
/// 通过注册表获取IE版本
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static int GetIeVerson()
|
|
{
|
|
int mainVer = (new WebBrowser()).Version.Major;
|
|
return mainVer;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置WebBrower IE默认版本
|
|
/// </summary>
|
|
/// <param name="version"></param>
|
|
public static void SetWebBrowerIe(int version)
|
|
{
|
|
int de = 0x1B58;
|
|
if (version == 8)
|
|
de = 0x1F40;
|
|
if (version == 9)
|
|
de = 0x2328;
|
|
if (version == 10)
|
|
de = 0x2710;
|
|
if (version == 11)
|
|
de = 0x2af8;
|
|
|
|
bool type = Environment.Is64BitOperatingSystem;//系统是否是64位
|
|
var exeName = Process.GetCurrentProcess().ProcessName + ".exe";
|
|
var hklm = Registry.LocalMachine;
|
|
string key = type
|
|
? @"SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION"
|
|
: @"SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
|
|
|
|
try
|
|
{
|
|
var lmRun = hklm.OpenSubKey(key, true);
|
|
if (lmRun == null)
|
|
return;
|
|
lmRun.SetValue(exeName, de); //设置webbrowser调用IE最高版本内核打开
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|