using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace MeshworkLinkMode
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("使用InternetGetConnectedState對網絡連接方式進行判斷");
Console.WriteLine(Fun_InternetGetConnectedState());
Console.WriteLine("使用IsNetworkAlive對網絡連接方式進行判斷");
Console.WriteLine(Fun_IsNetworkAlive());
Console.ReadLine();
}
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
[DllImport("sensapi.dll")]
private extern static bool IsNetworkAlive(out int connectionDescription);
static string Fun_InternetGetConnectedState()
{
int INTERNET_CONNECTION_MODEM = 1;
int INTERNET_CONNECTION_LAN = 2;
int INTERNET_CONNECTION_PROXY = 4;
int INTERNET_CONNECTION_MODEM_BUSY = 8;
string outPut = null;
int flags;//上網方式
bool m_bOnline = true;//是否在線
m_bOnline = InternetGetConnectedState(out flags, 0);
if (m_bOnline)//在線
{
if ((flags & INTERNET_CONNECTION_MODEM) == INTERNET_CONNECTION_MODEM)
{
outPut = "在線:撥號上網
";
}
if ((flags & INTERNET_CONNECTION_LAN) == INTERNET_CONNECTION_LAN)
{
outPut = "在線:通過局域網
";
}
if ((flags & INTERNET_CONNECTION_PROXY) == INTERNET_CONNECTION_PROXY)
{
outPut = "在線:代理
";
}
if ((flags & INTERNET_CONNECTION_MODEM_BUSY) == INTERNET_CONNECTION_MODEM_BUSY)
{
outPut = "MODEM被其他非INTERNET連接占用
";
}
}
else
{
outPut = "不在線
";
}
return outPut;
}
static string Fun_IsNetworkAlive()
{
int NETWORK_ALIVE_LAN = 0;
int NETWORK_ALIVE_WAN = 2;
int NETWORK_ALIVE_AOL = 4;
string outPut = null;
int flags;//上網方式
bool m_bOnline = true;//是否在線
m_bOnline = IsNetworkAlive(out flags);
if (m_bOnline)//在線
{
if ((flags & NETWORK_ALIVE_LAN) == NETWORK_ALIVE_LAN)
{
outPut = "在線:NETWORK_ALIVE_LAN
";
}
if ((flags & NETWORK_ALIVE_WAN) == NETWORK_ALIVE_WAN)
{
outPut = "在線:NETWORK_ALIVE_WAN
";
}
if ((flags & NETWORK_ALIVE_AOL) == NETWORK_ALIVE_AOL)
{
outPut = "在線:NETWORK_ALIVE_AOL
";
&nb