C#中判断本地系统的网络连接状态的方法
程序员文章站
2023-12-22 09:37:10
函数internetgetconnectedstate返回本地系统的网络连接状态。
语法:
bool internetgetconnectedstate(
__out...
函数internetgetconnectedstate返回本地系统的网络连接状态。
语法:
bool internetgetconnectedstate(
__out lpdword lpdwflags,
__in dword dwreserved
);
参数:
lpdwflags[out]
指向一个变量,该变量接收连接描述内容。该参数在函数返回flase时仍可以返回一个有效的标记。该参数可以为下列值的一个或多个。
值
|
含义
|
---|---|
internet_connection_configured0x40
|
local system has a valid connection to the internet, but it might or might not be currently connected.
|
internet_connection_lan 0x02
|
local system uses a local area network to connect to the internet.
|
internet_connection_modem0x01
|
local system uses a modem to connect to the internet.
|
internet_connection_modem_busy0x08
|
no longer used.
|
internet_connection_offline 0x20
|
local system is in offline mode.
|
internet_connection_proxy0x04
|
local system uses a proxy server to connect to the internet.
|
internet_ras_installed0x10
|
local system has ras installed.
|
dwreserved[in]
保留值。必须为0。
返回值:
当存在一个modem或一个lan连接时,返回true,当不存在internet连接或所有的连接当前未被激活时,返回false。
当该函数返回false时,程序可以调用getlasterror来接收错误代码。
示例:
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();
}
}
}
截图:
复制代码 代码如下:
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();
}
}
}
截图: