获取系统的网络状态与无线网的信号强度(格数),C# 获取电脑的网络连接状态

C#

浏览数:821

2019-6-14

一台电脑,有时通过有线网连接网络(调制解调器/局域网),如有网卡也可连接wifi。
那么如何获取WLAN是否连接,和相应的信号强度呢?

就以下俩点:

  • 获取电脑连网状态
  • 获取无线网络的信号强度

 获取电脑连网状态

通过IsNetworkAlive可获取当前环境是否已经连接网络。详见《C# 获取电脑的网络连接状态

流程:网络是否连接->是否有无线网连接->获取无线网状态(信号强度)->返回网络状态

 1 [DllImport("sensapi.dll", SetLastError = true)]
 2 private static extern bool IsNetworkAlive(out int connectionDescription);
 3 public NetworkStatus GetNetworkStatusByNetworkAlive()
 4 {
 5     var networkStatus = NetworkStatus.InternetWithError;
 6 
 7     int flags = 0;
 8     var isNetworkAlive = IsNetworkAlive(out flags);
 9 
10     int errCode = Marshal.GetLastWin32Error();
11     if (errCode != 0)
12     {
13         throw new InvalidOperationException($"通过{nameof(IsNetworkAlive)}非托管DLL,获取网络状态时,遇到异常");
14     }
15     if (isNetworkAlive)
16     {
17         // 获取WLAN网络状态
18         var wlanStatus = GetWlanStatus();
19         if (isNetworkAlive && wlanStatus == NetworkStatus.WifiWithErro)
20         {
21             networkStatus = NetworkStatus.Internet;
22         }
23         else
24         {
25             networkStatus = wlanStatus;
26         }
27     }
28     return networkStatus;
29 }

网络状态枚举值

暂时只定义了有线网和无线网的状态 

 1     /// <summary>
 2     /// 网络状态
 3     /// </summary>
 4     public enum NetworkStatus
 5     {
 6         Internet,
 7         InternetWithError,
 8         WifiWithErro,
 9         WifiWithOneBar,
10         WifiWithTwoBars,
11         WifiWithThreeBars,
12         WifiWithFourBars,
13     }

那么,具体是否有线/还是无线网络,如何判断?见下面~

获取无线网络的信号强度

获取WLAN的名称与信号强度

无线网的信息,可以通过第三方开源ManagedWifi来获取。

ManagedWifi.Dll也可以从我的云盘下载:https://pan.baidu.com/s/1CjSUIMr0DuVqDZrdZCx_mA 密码:2d2o

下载后引用到项目中

1. WlanClient wlanClient = new WlanClient();

2. 循环foreach(WlanClient.WlanInterface wlanIface in wlanClient.Interfaces)

3. Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);

值得注意的是,

  • WlanClient只在有网卡的系统中能初始化–所以如果WlanClient不能初始化,且IsNetworkAlive返回true,则意味着当前是有线连接网线
  • WlanInterfacer的GetAvailableNetworkList方法,获取的是附近wifi热点列表,和任务栏下热点列表的顺序无关。
  • WlanAvailableNetwork的wlanSignalQuality信号强度,100以内,如要转换成信号格数,需要整除25
  • profileName,对已连接的热点profileName显示完整,未连接的热点profileName为empty.

获取无线网连接状态:

 1 private NetworkStatus GetWlanStatus()
 2 {
 3     var wlanStatus = NetworkStatus.WifiWithErro;
 4     try
 5     {
 6         WlanClient wlanClient = new WlanClient();
 7 
 8         foreach (WlanClient.WlanInterface wlanIface in wlanClient.Interfaces)
 9         {
10             if (wlanIface.InterfaceState == Wlan.WlanInterfaceState.Connected && wlanIface.CurrentConnection.isState == Wlan.WlanInterfaceState.Connected)
11             {
12                 Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
13                 foreach (var network in networks)
14                 {
15                     if (network.profileName == wlanIface.CurrentConnection.profileName
16                         && (int)network.flags == (int)(Wlan.WlanAvailableNetworkFlags.Connected | Wlan.WlanAvailableNetworkFlags.HasProfile))
17                     {
18                         switch (network.wlanSignalQuality / 25)
19                         {
20                             case 0:
21                                 wlanStatus = NetworkStatus.WifiWithOneBar;
22                                 break;
23                             case 1:
24                                 wlanStatus = NetworkStatus.WifiWithTwoBars;
25                                 break;
26                             case 2:
27                                 wlanStatus = NetworkStatus.WifiWithThreeBars;
28                                 break;
29                             default:
30                                 wlanStatus = NetworkStatus.WifiWithFourBars;
31                                 break;
32                         }
33                         break;
34                     }
35                 }
36                 break;
37             }
38         }
39     }
40     catch (Exception e)
41     {
42     }
43     return wlanStatus;
44 }

关键字:网络连接状态NetworkStatus,无线网络强度(格数),ManagedWifi,IsNetworkAlive,GetLastError

作者:唐宋元明清2188