SoFunction
Updated on 2025-03-03

Summary of the method of reading local network configuration information in C#

Application scenarios

  1. Network diagnostic tools
    When developing network diagnostic tools, you need to obtain information about the local network interface, such as IP address, subnet mask, default gateway, etc., to help diagnose network connection problems.
  2. Dynamic network configuration
    In some application scenarios, such as cloud computing or containerized deployment, it may be necessary to dynamically configure network settings based on the current environment, such as automatically configuring IP addresses or updating DNS server addresses.
  3. Monitoring and logging
    Network configuration information is critical to monitoring network status and recording network activity logs. Obtaining this information through programs can help developers or system administrators better understand network behavior and identify potential problems.

Example

Here are several examples of using C# to read local network configuration information.

Example 1: Get information about all network interfaces

This example shows how to obtain basic information about all network interfaces on the local computer.

using System;
using ;
using ;
class Program
{
    static void Main()
    {        
        StringBuilder sb = new StringBuilder();
        ($"Local network interface information:");// Get and traverse all network interfaces        foreach (NetworkInterface ni in ())
        {
            ($"name: {}");
            ($"describe: {}");
            ($"state: {}");
            ($"MAC address: {()}");
            ("=======================================");
        }
        (());
     }
}

Example 2: Get IP configuration information for a specific network interface

This example shows how to get the IP address, subnet mask, and default gateway for a specified network interface.

using System;
using ;
using ;
using ;
using ;
class Program
{
    static void Main()
    {        
        // Specify the network interface name to retrieve        string interfaceName = "Wi-Fi";
        
        var networkInterface = ().FirstOrDefault(ni =>  == interfaceName);
        string message = "";
        
        if (networkInterface != null) {
            message += $"Network interface: {}\n";
            var ipProperties = ();
            
            // Get IPv4 configuration information            var ipv4Properties = (ua =>  == );
            if (ipv4Properties != null) {
                message += $"IP address: {}\n";
                message += $"Subnet mask: {ipv4Properties.IPv4Mask}\n";
            }
            
            // Get the default gateway            var gatewayAddress = (ga =>  == );
            if (gatewayAddress != null) {
                message += $"Default gateway: {}\n";
            }
        } else {
            message = "The specified network interface is not found.";
        }
        (message);
     }
}

Example 3: Get DNS server address

This example shows how to obtain and display the DNS server address configured by the local network interface.

using System;
using ;
using ;
using ;
class Program
{
    static void Main()
    {
        // Select an active network interface        var activeInterface = ().FirstOrDefault(ni =>  == );

        string message = "";
        if (activeInterface != null) {
            message += $"Network interface: {}\n";
            var ipProperties = ();
        
            // Get the DNS server address            var dnsAddresses = ;
            foreach (var dns in dnsAddresses) {
                message += $"DNSServer address: {dns}\n";
            }
        } else {
            message = "The active network interface was not found.";
        }
        (message, "Network Information", , );
    }
}

The above example shows how to read local network configuration information in C#, including basic information of the network interface, IP configuration, and DNS server address. Through this information, developers can develop feature-rich web applications to meet different business needs.

This is the article about how to read local network configuration information in C#. For more information about C# reading network configuration information, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!