NetworkChange 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
允許應用程式在網路介面 (也稱為網路卡或介面卡) 的網際網路通訊協定 (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 |
當網路的可用性變更時發生。 |