`
mmdev
  • 浏览: 12954772 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

C#网络连接情况

 
阅读更多

C#

1、导入win32 api

[DllImport("wininet")]

public static extern int InternetGetConnectedState(ref int lpdwFlags, int dwReserved);

2、用法如下:

string InternetGetConnectedStateString()

{

string strState = "";

try

{

int nState = 0;

// check internet connection state

if(InternetGetConnectedState(ref nState, 0) == 0)

return "You are currently not connected to the internet";

if((nState & 1) == 1)

strState = "Modem connection";

else if((nState & 2) == 2)

strState = "LAN connection";

else if((nState & 4) == 4)

strState = "Proxy connection";

else if((nState & 8) == 8)

strState = "Modem is busy with a non-Internet connection";

else if((nState & 0x10) == 0x10)

strState = "Remote Access Server is installed";

else if((nState & 0x20) == 0x20)

return "Offline";

else if((nState & 0x40) == 0x40)

return "Internet connection is currently configured";

// get current machine IP

IPHostEntry he = Dns.Resolve(Dns.GetHostName());

strState += ", Machine IP: "+he.AddressList[0].ToString();

}

catch

{

}

return strState;

}

Keyword:网络连接情况InternetGetConnectedState C#

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics