111,126
社区成员
发帖
与我相关
我的任务
分享
[Flags]
public enum InternetState
{
INTERNET_CONNECTION_MODEM = 0x01,
INTERNET_CONNECTION_LAN = 0x02,
INTERNET_CONNECTION_PROXY = 0x04,
INTERNET_CONNECTION_MODEM_BUSY = 0x08,
INTERNET_RAS_INSTALLED = 0x10,
INTERNET_CONNECTION_OFFLINE = 0x20,
INTERNET_CONNECTION_CONFIGURED = 0x40
}
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
public static extern bool InternetGetConnectedStateEx(out InternetState lpdwFlags, StringBuilder lpszConnectionName, int dwNameLen, int dwReserved);
static void Main(string[] args)
{
InternetState connectionState;
StringBuilder connectionName = new StringBuilder(256);
bool isOnline = InternetGetConnectedStateEx(out connectionState, connectionName, 256, 0);
MessageBox.Show(isOnline.ToString());
MessageBox.Show(connectionState.ToString());
MessageBox.Show(connectionName.ToString());
}
using System;
using System.Runtime.InteropServices;
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
//Creating a function that uses the API function...
public static bool IsConnectedToInternet()
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
//Use function
if (IsConnectedToInternet())
{
MessageBox.Show("Netwok Connection Up");
}
else
{
MessageBox.Show("Sorry! Network Connection down.");
}