//判断计算机是否能够连接到Internet的API
//wininet.dll是Windows应用程序网络相关模块。该文件隶属于%WINDOWSSYSTEM32目录下动态库连接文件。该文件不可缺失,属于关键链接库。当文件丢失或者损坏时,届时将无法完成Explorer进程(即桌面以及基于该进程的IE浏览器等,可使用不基于IE浏览器进行访问网络)
[DllImport("wininet.dll")]
public extern static bool InternetGetConnectedState( outint Description, int ReservedValue ) ;
//创建一个调用API函数(封装为C#方式).
///<summary>
/// 查看网络是否连接到公网
///</summary>
///<returns>返回Ture:可以连接到Internet,False则连接不上</returns>
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState(out Desc, 0);
}
第二种设置永久cookie
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 设置cookie
/// </summary>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
/// <summary>
/// 获取cookie
/// </summary>
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetGetCookie(
string url, string name, StringBuilder data, ref int dataSize);
static void Main(string[] args)
{
//获取旧的
StringBuilder cookie = new StringBuilder(new String(' ',2048));
int datasize = cookie.Length;
bool b= InternetGetCookie("http://community.csdn.net", null, cookie, ref datasize);
//删除旧的
foreach (string fileName in System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies)))
{
if (fileName.ToLower().IndexOf("csdn") > 0)
{
System.IO.File.Delete("csdn");
}
}
//生成新的
foreach (string c in cookie.ToString().Split(';'))
{
string[] item = c.Split('=');
string name = item[0];
string value = item[1] + ";expires=Sun,22-Feb-2099 00:00:00 GMT";
InternetSetCookie("http://community.csdn.net",name,value);
InternetSetCookie("http://forum.csdn.net", name, value);
InternetSetCookie("http://webim.csdn.net", name, value);
}
}
}
}
若要免登录可以调用 免登录
Process.Start("iexplore.exe", LinkAddress.CookiesFind);