NetworkChange.NetworkAddressChanged Événement

Définition

Se produit lorsque l'adresse IP d'une interface réseau change.Occurs when the IP address of a network interface changes.

public:
 static event System::Net::NetworkInformation::NetworkAddressChangedEventHandler ^ NetworkAddressChanged;
public static event System.Net.NetworkInformation.NetworkAddressChangedEventHandler NetworkAddressChanged;
member this.NetworkAddressChanged : System.Net.NetworkInformation.NetworkAddressChangedEventHandler 
Public Shared Custom Event NetworkAddressChanged As NetworkAddressChangedEventHandler 

Exemples

L’exemple de code suivant écoute les modifications d’adresse et affiche l’état des interfaces réseau lorsqu’un événement NetworkAddressChanged se produit.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

Remarques

La classe NetworkChange déclenche des événements NetworkAddressChanged lorsque l’adresse d’une interface réseau, également appelée carte réseau ou adaptateur, change.The NetworkChange class raises NetworkAddressChanged events when the address of a network interface, also called a network card or adapter, changes.

Pour qu’un objet NetworkChange appelle une méthode de gestion d’événements lorsqu’un événement NetworkAddressChanged se produit, vous devez associer la méthode à un délégué NetworkAddressChangedEventHandler et ajouter ce délégué à cet événement.To have a NetworkChange object call an event-handling method when a NetworkAddressChanged event occurs, you must associate the method with a NetworkAddressChangedEventHandler delegate, and add this delegate to this event.

S’applique à