none
如何使用c# 代码检测到 WIFI连接状态,特别是出现黄色感叹号的时候 RRS feed

答案

  • Hi,

    从NetworkChange Class 这个类中,不难看出对网线拔掉,是可以检测的。

    The NetworkChange class provides address change notification by raising NetworkAddressChanged events. An interface address can change for many reasons, such as a disconnected network cable, moving out of range of a wireless Local Area Network, or hardware failure.

    如果不可以,你尝试用ping的方法来代替呢?

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Hank1983 2018年1月18日 11:29
    2018年1月17日 7:57
    版主

全部回复

  • 参考下 Native Wifi API Sample

    https://msdn.microsoft.com/en-us/library/windows/desktop/aa816369(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/ms706556(v=vs.85).aspx

    这里还有个DMO

    https://blogs.msdn.microsoft.com/winsdk/2015/01/09/wlan-programming-how-to-tips-and-tricks-including-using-it-in-c/


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年1月16日 0:46
  • Hi,

    欢迎在MSDN论坛发帖。

    据我所知在C#中是有个 System.Net.NetworkInformation;类的,它是可以检查网络的状态。 建议你尝试一下。

    这个NetworkChange 类提供一个检测地址改变的通知,通过触发NetworkAddressChanged 事件,这个地址改变是指网线没有链接,把电脑移出域网之外了,这些都可以检测出来。

    下面这个代码,你可以测试一下。

    using System;
    using System.Net;
    using System.Net.NetworkInformation;
    
    namespace Examples.Net.AddressChanges
    {
        public class NetworkingExample
        {
            public static void Main()
            {
                NetworkChange.NetworkAddressChanged += new 
                NetworkAddressChangedEventHandler(AddressChangedCallback);
                Console.WriteLine("Listening for address changes. Press any key to exit.");
                Console.ReadLine();
            }
            static void AddressChangedCallback(object sender, EventArgs e)
            {
    
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach(NetworkInterface n in adapters)
                {
                    Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
                }
            }
        }
    }

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年1月16日 2:11
    版主
  • Hi,

    欢迎在MSDN论坛发帖。

    据我所知在C#中是有个 System.Net.NetworkInformation;类的,它是可以检查网络的状态。 建议你尝试一下。

    这个NetworkChange 类提供一个检测地址改变的通知,通过触发NetworkAddressChanged 事件,这个地址改变是指网线没有链接,把电脑移出域网之外了,这些都可以检测出来。

    下面这个代码,你可以测试一下。

    using System;
    using System.Net;
    using System.Net.NetworkInformation;
    
    namespace Examples.Net.AddressChanges
    {
        public class NetworkingExample
        {
            public static void Main()
            {
                NetworkChange.NetworkAddressChanged += new 
                NetworkAddressChangedEventHandler(AddressChangedCallback);
                Console.WriteLine("Listening for address changes. Press any key to exit.");
                Console.ReadLine();
            }
            static void AddressChangedCallback(object sender, EventArgs e)
            {
    
                NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
                foreach(NetworkInterface n in adapters)
                {
                    Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus);
                }
            }
        }
    }

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    从网络正常状态,拔掉路由器网线,出现黄色感叹号无提示。

    从黄色感叹号到网络自动接通,有提示。可惜我需要的是前者……。

    2018年1月16日 14:37
  • Hi,

    从NetworkChange Class 这个类中,不难看出对网线拔掉,是可以检测的。

    The NetworkChange class provides address change notification by raising NetworkAddressChanged events. An interface address can change for many reasons, such as a disconnected network cable, moving out of range of a wireless Local Area Network, or hardware failure.

    如果不可以,你尝试用ping的方法来代替呢?

    Best Regards,

    Hart


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 Hank1983 2018年1月18日 11:29
    2018年1月17日 7:57
    版主