IPNetworkChange Class

Namespace: System.Net.IP
Assembly: System.Net.Primitives.dll

Represents a class that notifies when network address changes occur.

  • Inheritance: Object → IPNetworkChange
  • Namespace: System.Net.IP
  • Assembly: System.Net.Primitives.dll

Syntax

public static class IPNetworkChange

Remarks

The IPNetworkChange class provides a mechanism to subscribe to and receive notifications about network interface address changes. This is useful for applications that need to react to the addition, removal, or modification of IP addresses on the local machine.

You can subscribe to the following events:

These events are static, meaning you can access them directly through the IPNetworkChange class without creating an instance of the class.

Events

NetworkAddressChanged

public static event NetworkAddressChangedEventHandler NetworkAddressChanged;

Occurs when the network address changes.

Remarks

This event is raised when an IP address is added, removed, or changed on any network interface. The NetworkAddressChangedEventHandler delegate is used for this event.

Returns

An NetworkAddressChangedEventHandler delegate.

Example

using System;
using System.Net.Sockets;

public class NetworkListener
{
    public static void Main(string[] args)
    {
        System.Net.IPNetworkChange.NetworkAddressChanged += NetworkAddressChangedHandler;
        Console.WriteLine("Listening for network address changes...");
        Console.WriteLine("Press Enter to exit.");
        Console.ReadLine();

        System.Net.IPNetworkChange.NetworkAddressChanged -= NetworkAddressChangedHandler;
    }

    private static void NetworkAddressChangedHandler(object sender, EventArgs e)
    {
        Console.WriteLine("Network address changed detected!");
        // You can add logic here to check for new IP addresses or other changes
    }
}

InputAddressChanged

public static event NetworkAddressChangedEventHandler InputAddressChanged;

Occurs when the input address changes.

Remarks

This event is raised when an IP address is added, removed, or changed on an input network interface. The NetworkAddressChangedEventHandler delegate is used for this event.

Returns

An NetworkAddressChangedEventHandler delegate.

Methods

GetAddresses

public static IPAddress[] GetAddresses(bool throwOnError = true)

Retrieves the list of IP addresses configured on the local machine.

Parameters

  • throwOnError: A boolean value that indicates whether to throw an exception if an error occurs. Defaults to true.

Returns

An array of IPAddress objects representing the local IP addresses. Returns an empty array if no addresses are found or if throwOnError is false and an error occurs.

Remarks

This method can be used to get the current state of the network interfaces and their associated IP addresses.

GetNetworkInterfaces

public static IPNetworkInterface[] GetNetworkInterfaces()

Retrieves the list of network interfaces on the local machine.

Returns

An array of IPNetworkInterface objects, each representing a network interface.