NetworkChange 类
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
允许应用程序在网络接口 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 委托相关联,并将此委托添加到事件。
Network |
已过时.
初始化 NetworkChange 类的新实例。 |
Register |
已过时.
注册网络更改实例来接收网络更改事件。 |
Network |
在网络接口的 IP 地址更改时发生。 |
Network |
在网络的可用性更改时发生。 |
产品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |