NetworkChange 類別

定義

允許應用程式在網路介面 (也稱為網路卡或介面卡) 的網際網路通訊協定 (IP) 位址變更時收到通知。Allows applications to receive notification when the Internet Protocol (IP) address of a network interface, also called a network card or adapter, changes.

public ref class NetworkChange sealed
public sealed class NetworkChange
type NetworkChange = class
Public NotInheritable Class NetworkChange
繼承
NetworkChange

範例

下列程式碼範例會接聽位址變更,並在發生 NetworkAddressChanged 事件時,顯示網路介面的狀態。The following code example listens for address changes and displays the status of network interfaces when a NetworkAddressChanged event occurs.

#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 事件來提供位址變更通知。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.

若要接收通知,您必須識別應用程式的事件處理常式,這是每次引發事件時,執行應用程式特定工作的一或多個方法。To receive notification, you must identify your application's event handlers, which are one or more methods that perform your application-specific tasks each time the event is raised. 若要讓 NetworkChange 物件在發生 NetworkAddressChanged 事件時呼叫您的事件處理方法,您必須將方法與 NetworkAddressChangedEventHandler 委派建立關聯,並將這個委派加入至事件。To have a NetworkChange object call your event-handling methods when a NetworkAddressChanged event occurs, you must associate the methods with a NetworkAddressChangedEventHandler delegate, and add this delegate to the event.

建構函式

NetworkChange()

NetworkChange 類別的新執行個體初始化。Initializes a new instance of the NetworkChange class.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
RegisterNetworkChange(NetworkChange)

註冊網路變更執行個體以接收網路變更事件。Registers a network change instance to receive network change events.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

事件

NetworkAddressChanged

當網路介面的 IP 位址變更時發生。Occurs when the IP address of a network interface changes.

NetworkAvailabilityChanged

當網路的可用性變更時發生。Occurs when the availability of the network changes.

適用於