检查网络本地/无线/调制解调器连接是否处于活动状态 [英] Checking for network Local/Wireless/Modem connections being active

查看:112
本文介绍了检查网络本地/无线/调制解调器连接是否处于活动状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

VS 2008 SP1

VS 2008 SP1

我正在使用下面的代码来测试用户是通过无线连接还是通过LAN连接.即电缆已插入,或者无线设备已关闭.该代码为此工作正常.但是,如果您能发现与此相关的任何潜在问题,或者您知道一种更好的方法,那么我将有兴趣了解更多信息.

I am using the code below to test whether the user is connected using either a wireless or LAN connection. i.e. that the cable is plugged in, or the wireless is switch off. The code works fine for this. However, if you can spot any potential problems with this or you know of a better way I would be interested to learn more.

但是,客户端希望我们也检查用户是否已使用调制解调器连接.如您从我的源代码中看到的,我正在寻找以"Local Area Connection"或"Local Area Connection"开头的连接.或无线网络连接".没关系.

However, the client would like us to check if the user is connected using a modem as well. As you can see from my source code I am looking for connection that start with either "Local Area Connection" or "Wireless Network Connection". This is ok.

但是,问题在于调制解调器名称可以是任何名称,因为当用户使用新连接向导"设置其调制解调器连接时,他们可以将其命名为任何名称.因此,如果我的switch语句不知道要查找什么.

However, the problem is that the modem name could be anything, as when the user sets up their modem connection using the 'new connection wizard' they can call this anything. So if my switch statement I don't know what to look for.

任何建议都是最有帮助的,

Any suggestions would be most helpfull,

 // Checks if Network is either connected by LAN or Wireless
        public bool IsNetworkConnected()
        {
            NetworkInterface[] networkCards = NetworkInterface.GetAllNetworkInterfaces();
            bool connected = false;

            // Loop through to find the one we want to check for connectivity.
            // Connection can have different numbers appended so check that the 
            // network connections start with the conditions checked below.
            foreach (NetworkInterface nc in networkCards)
            {
                // Check LAN
                if (nc.Name.StartsWith("Local Area Connection"))
                {
                    if (nc.OperationalStatus == OperationalStatus.Up)
                    {
                        connected = true;
                    }
                }

                // Check for Wireless
                if (nc.Name.StartsWith("Wireless Network Connection"))
                {
                    if (nc.OperationalStatus == OperationalStatus.Up)
                    {
                        connected = true;
                    }
                }
            }

            return connected;
        }

推荐答案

您好,

您可以使用WinAPI来检查连接状态.

using System.Runtime.InteropServices;

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int flags, int reserved);

public bool IsNetworkConnected()
{
    int flags;
    return InternetGetConnectedState(out flags, 0);
}


还可以检查带有标志值的连接类型:
http://msdn.microsoft.com/zh-CN/library/aa384702(VS.85).aspx

在我的计算机上,我看到带有值0x12的标志(wifi-它不在MSDN文档中).


You can also check connection type with flags value:
http://msdn.microsoft.com/en-us/library/aa384702(VS.85).aspx

On my computer I see flags with value 0x12 (wifi - it's not in MSDN document).


这篇关于检查网络本地/无线/调制解调器连接是否处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆