NetworkChange 类

定义

允许应用程序在网络接口 Internet 协议 (IP) 地址(也称为网卡或适配器)更改时接收通知。

public ref class NetworkChange abstract sealed
public ref class NetworkChange
public ref class NetworkChange sealed
public static class NetworkChange
public class NetworkChange
public sealed class NetworkChange
type NetworkChange = class
Public Class NetworkChange
Public NotInheritable Class NetworkChange
继承
NetworkChange

示例

下面的代码示例侦听地址更改,并在事件发生时 NetworkAddressChanged 显示网络接口的状态。

#using <System.dll>

using namespace System;
using namespace System::Net;
using namespace System::Net::NetworkInformation;
void AddressChangedCallback( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   array<NetworkInterface^>^adapters = NetworkInterface::GetAllNetworkInterfaces();
   System::Collections::IEnumerator^ myEnum = adapters->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      NetworkInterface^ n = safe_cast<NetworkInterface^>(myEnum->Current);
      Console::WriteLine( "   {0} is {1}", n->Name, n->OperationalStatus );
   }
}

int main()
{
   NetworkChange::NetworkAddressChanged += gcnew NetworkAddressChangedEventHandler( AddressChangedCallback );
   Console::WriteLine( "Listening for address changes. Press any key to exit." );
   Console::ReadLine();
}
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);
            }
        }
    }
}
Imports System.Net
Imports System.Net.NetworkInformation

Public Class NetworkingExample
    Public Shared Sub Main()
        AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
        Console.WriteLine("Listening for address changes. Press any key to exit.")
        Console.ReadLine()
    End Sub
    Private Shared Sub AddressChangedCallback(ByVal sender As Object, ByVal e As EventArgs)

        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim n As NetworkInterface
        For Each n In adapters
            Console.WriteLine("   {0} is {1}", n.Name, n.OperationalStatus)
        Next n
    End Sub
End Class

注解

NetworkChange 通过引发 NetworkAddressChanged 事件提供地址更改通知。 接口地址可能会由于多种原因而更改,例如断开连接的网络电缆、移动出无线局域网的范围或硬件故障。

若要接收通知,必须标识应用程序的事件处理程序,这些事件处理程序是每次引发事件时执行特定于应用程序的任务的一个或多个方法。 若要使对象在 NetworkChange 事件发生时 NetworkAddressChanged 调用事件处理方法,必须将方法与 NetworkAddressChangedEventHandler 委托相关联,并将此委托添加到事件。

构造函数

NetworkChange()
已过时.

初始化 NetworkChange 类的新实例。

方法

RegisterNetworkChange(NetworkChange)
已过时.

注册网络更改实例来接收网络更改事件。

事件

NetworkAddressChanged

在网络接口的 IP 地址更改时发生。

NetworkAvailabilityChanged

在网络的可用性更改时发生。

适用于