Introduction
When building desktop network diagnostics, IoT onboarding tools, or local service monitors in .NET, hardcoding a router address such as 192.168.1.1 breaks quickly in real environments. Consumer routers, ISP fiber ONTs, and office VLANs ship with different private address schemes (192.168.0.1, 10.0.0.1, 192.168.1.254), while developer machines running Docker, WSL, or VPN clients expose multiple active adapters simultaneously.
Parsing ipconfig or ip route output introduces fragile string matching and locale issues across operating systems. Instead, .NET lets you read adapter state, assigned default gateways, and IPv4 subnet masks directly through System.Net.NetworkInformation.
Prerequisites
.NET 6.0 or later (tested on .NET 8)
Visual Studio 2022, VS Code, or the .NET CLI
Basic familiarity with C# console applications and IPv4 addressing
Enumerating Active IPv4 Gateways Safely
Calling NetworkInterface.GetAllNetworkInterfaces() returns every physical and virtual network adapter on the host. Checking OperationalStatus.Up alone is not enough, because loopback interfaces, tunnel adapters, and internal virtual switches (Hyper-V, Docker, WSL) often report Up even when they do not route external traffic.
Some virtual and VPN adapters also report 0.0.0.0 (IPAddress.Any) as a placeholder gateway. The following console example filters those out before printing active host IPs and subnet masks:
using System;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
public static class GatewayInspector
{
public static void Main()
{
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus != OperationalStatus.Up)
continue;
if (nic.NetworkInterfaceType == NetworkInterfaceType.Loopback ||
nic.NetworkInterfaceType == NetworkInterfaceType.Tunnel)
continue;
IPInterfaceProperties props = nic.GetIPProperties();
var ipv4Gateways = props.GatewayAddresses
.Select(g => g.Address)
.Where(addr => addr.AddressFamily == AddressFamily.InterNetwork &&
!addr.Equals(IPAddress.Any))
.ToList();
// Skip active virtual switches (Docker, WSL, Hyper-V) with no default gateway
if (ipv4Gateways.Count == 0)
continue;
Console.WriteLine($"Adapter : {nic.Name} ({nic.NetworkInterfaceType})");
foreach (IPAddress gateway in ipv4Gateways)
{
Console.WriteLine($"Gateway : {gateway}");
}
foreach (UnicastIPAddressInformation uni in props.UnicastAddresses)
{
if (uni.Address.AddressFamily != AddressFamily.InterNetwork)
continue;
IPAddress mask = uni.IPv4Mask;
string maskDisplay = (mask is null || mask.Equals(IPAddress.Any))
? "Unavailable"
: mask.ToString();
Console.WriteLine($"Host IP : {uni.Address} | Mask: {maskDisplay}");
}
Console.WriteLine(new string('-', 50));
}
}
}Output
On a workstation connected to Wi-Fi alongside active WSL and Docker virtual adapters, virtual switches are excluded automatically because their GatewayAddresses collections contain no valid IPv4 gateway:
Adapter : Wi-Fi (Wireless80211)
Gateway : 192.168.1.1
Host IP : 192.168.1.42 | Mask: 255.255.255.0
--------------------------------------------------
Join the conversation! Your thoughts help the community grow.