DH.NetworkInfo
1.0.1
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Framework 4.6
This package targets .NET Framework 4.6. The package is compatible with this framework or higher.
dotnet add package DH.NetworkInfo --version 1.0.1
NuGet\Install-Package DH.NetworkInfo -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DH.NetworkInfo" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DH.NetworkInfo" Version="1.0.1" />
<PackageReference Include="DH.NetworkInfo" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DH.NetworkInfo --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DH.NetworkInfo, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DH.NetworkInfo@1.0.1
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DH.NetworkInfo&version=1.0.1
#tool nuget:?package=DH.NetworkInfo&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
DH.NetworkInfo
DH.NetworkInfo is a managed Windows network information library for C# developers.
This package is Windows-only.
Features
- Default IP / primary adapter lookup — picks the adapter with a usable default gateway, so you get the one active adapter that actually routes traffic instead of just the first adapter in the list
- Ordered default-gateway adapter list for multi-GW environments
- Adapter enumeration
- Default route enumeration
- IPv4 route table enumeration
- Typed values and display text in the same managed object model
- DHCP, DHCPv6, WINS, DNS suffix, and NetBIOS-related adapter fields
- Strong-name signed managed assembly starting from
1.0.1
Quick Start
using DH.NetworkInfo;
DHNetworkInfo networkInfo = new DHNetworkInfo();
if (!networkInfo.Refresh(DHNetworkInfoRefreshOptions.AllDetails)) {
return;
}
// Primary adapter = the adapter with a usable IPv4 address AND a usable default gateway.
// When multiple adapters qualify, the one with the lowest route metric wins.
AdapterInfo primaryAdapter = networkInfo.GetPrimaryAdapter();
if (primaryAdapter != null) {
string ipv4 = primaryAdapter.IPv4Address; // the machine's "default IP"
uint ipv4Value = primaryAdapter.IPv4Value;
string gateway = primaryAdapter.Gateway; // the default gateway that qualified this adapter
}
Two ways to call the API
DHNetworkInfo(instance, shown above) — callRefresh()once and reuse the cached snapshot throughPrimaryAdapter,Adapters,DefaultRoutes,Routes, and theGet*helper methods.DHNetworkInfoReader(static) — same underlying data collection, called directly with no instance or caching, useful for a single one-off lookup:
using DH.NetworkInfo;
AdapterInfo primaryAdapter = DHNetworkInfoReader.GetPrimaryAdapter();
IReadOnlyList<AdapterInfo> adapters = DHNetworkInfoReader.GetAdapters();
IReadOnlyList<AdapterInfo> gatewayAdapters = DHNetworkInfoReader.GetDefaultGatewayAdapters();
IReadOnlyList<RouteInfo> defaultRoutes = DHNetworkInfoReader.GetDefaultRoutes();
DHNetworkInfo.Refresh(options) internally calls DHNetworkInfoReader.GetSnapshot(options), so both
styles return identical data — pick whichever fits your usage pattern.
Multi-gateway interpretation
GetPrimaryAdapter()returns the one adapter selected as the machine's current default path.GetDefaultGatewayAdapters()returns every adapter that currently exposes a usable gateway.GetDefaultRoutes()returns all current default-route candidates.- The default-route list is ordered by ascending
Metric. - If multiple default routes share the same
Metric, the route attached to the selected primary adapter is listed first. - The default-gateway adapter list starts in that same route-priority order, then appends any remaining gateway-backed adapters.
using DH.NetworkInfo;
DHNetworkInfo networkInfo = new DHNetworkInfo();
if (!networkInfo.Refresh(DHNetworkInfoRefreshOptions.AllDetails)) {
return;
}
AdapterInfo primaryAdapter = networkInfo.GetPrimaryAdapter();
IReadOnlyList<RouteInfo> defaultRoutes = networkInfo.DefaultRoutes;
if (primaryAdapter != null) {
Console.WriteLine("Primary Adapter : " + primaryAdapter.FriendlyName);
Console.WriteLine("Primary IPv4 : " + primaryAdapter.IPv4Address);
Console.WriteLine("Gateway : " + primaryAdapter.Gateway);
}
for (int index = 0; index < defaultRoutes.Count; ++index) {
RouteInfo route = defaultRoutes[index];
Console.WriteLine("#{0} nextHop={1} if={2} metric={3}",
index,
route.NextHop,
route.InterfaceAddress,
route.Metric);
}
Included Samples
DH.NetworkInfoSampleCSharpDH.IPConfigAllCSharp
Notes
- The package targets
net46,net48,net6.0-windows, andnet8.0-windows. - The managed package does not depend on the native C++ DLL.
- The managed package is Windows-only and uses Windows networking APIs directly.
- Starting with
1.0.1, the package is strong-name signed. Consumers upgrading from unsigned1.0.0may need to rebuild dependent projects. - DHCP lease timestamps follow the documented public adapter API values exposed by Windows.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0-windows7.0 is compatible. net7.0-windows was computed. net8.0-windows was computed. net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
| .NET Framework | net46 is compatible. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 is compatible. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.6
- No dependencies.
-
.NETFramework 4.8
- No dependencies.
-
net6.0-windows7.0
- No dependencies.
-
net8.0-windows7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.