示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
private const int INTERNET_CONNECTION_MODEM = 1;
private const int INTERNET_CONNECTION_LAN = 2;
private const int INTERNET_CONNECTION_PROXY = 4;
private const int INTERNET_CONNECTION_MODEM_BUSY = 8;
[DllImport("winInet.dll ")]
//声明外部的函数:
private static extern bool InternetGetConnectedState(
ref int Flag,
int dwReserved
);
static void Main(string[] args)
{
int Flag = 0;
string netStates = "";
if (!InternetGetConnectedState(ref Flag, 0))
{
Console.WriteLine("no!");
}
else
{
if ((Flag & INTERNET_CONNECTION_MODEM) != 0)
netStates += " Connect by MODEM /n";
if ((Flag & INTERNET_CONNECTION_LAN) != 0)
netStates += "Connect by LAN /n";
if ((Flag & INTERNET_CONNECTION_PROXY) != 0)
netStates += "Connect by PROXY /n";
if ((Flag & INTERNET_CONNECTION_MODEM_BUSY) != 0)
netStates += " MODEM is busy /n";
}
Console.WriteLine(netStates);
Console.ReadLine();
}
}
}
截图: