Skip to content

Commit c92297f

Browse files
committed
Docs: #3288
1 parent 1d1e291 commit c92297f

File tree

1 file changed

+91
-46
lines changed

1 file changed

+91
-46
lines changed

Source/NETworkManager.Models/Network/NetworkInterface.cs

Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4-
using System.Diagnostics;
54
using System.Linq;
65
using System.Net;
76
using System.Net.NetworkInformation;
@@ -13,8 +12,15 @@
1312
namespace NETworkManager.Models.Network;
1413

1514
/// <summary>
16-
/// Provides functionality to manage network interfaces.
15+
/// Provides static and instance methods for retrieving information about network interfaces, detecting local IP
16+
/// addresses and gateways, and configuring network interface settings on the local machine.
1717
/// </summary>
18+
/// <remarks>The NetworkInterface class offers both synchronous and asynchronous methods for enumerating network
19+
/// interfaces, detecting routing and gateway information, and performing network configuration tasks such as setting IP
20+
/// addresses, DNS servers, and flushing the DNS cache. Most configuration operations require administrative privileges.
21+
/// Events are provided to notify when user-initiated cancellations occur, such as when a UAC prompt is dismissed. This
22+
/// class is intended for use in applications that need to query or modify network interface settings
23+
/// programmatically.</remarks>
1824
public sealed class NetworkInterface
1925
{
2026
#region Variables
@@ -37,18 +43,24 @@ public sealed class NetworkInterface
3743
#region Methods
3844

3945
/// <summary>
40-
/// Gets a list of all available network interfaces asynchronously.
46+
/// Asynchronously retrieves a list of available network interfaces on the local machine.
4147
/// </summary>
42-
/// <returns>A task that represents the asynchronous operation. The task result contains a list of <see cref="NetworkInterfaceInfo"/>.</returns>
48+
/// <returns>A task that represents the asynchronous operation. The task result contains a list of <see
49+
/// cref="NetworkInterfaceInfo"/> objects describing each detected network interface.</returns>
4350
public static Task<List<NetworkInterfaceInfo>> GetNetworkInterfacesAsync()
4451
{
4552
return Task.Run(GetNetworkInterfaces);
4653
}
4754

4855
/// <summary>
49-
/// Gets a list of all available network interfaces.
56+
/// Retrieves a list of network interfaces on the local machine, including detailed information about each interface
57+
/// such as addresses, gateways, and DHCP settings.
5058
/// </summary>
51-
/// <returns>A list of <see cref="NetworkInterfaceInfo"/> describing the available network interfaces.</returns>
59+
/// <remarks>Only Ethernet, Wireless80211, and proprietary virtual/internal interfaces are included. The
60+
/// returned information includes both IPv4 and IPv6 details, as well as DHCP and DNS configuration where available.
61+
/// This method may require appropriate permissions to access network configuration data.</remarks>
62+
/// <returns>A list of <see cref="NetworkInterfaceInfo"/> objects, each representing a network interface with its associated
63+
/// properties. The list is empty if no matching interfaces are found.</returns>
5264
public static List<NetworkInterfaceInfo> GetNetworkInterfaces()
5365
{
5466
List<NetworkInterfaceInfo> listNetworkInterfaceInfo = [];
@@ -182,21 +194,29 @@ public static List<NetworkInterfaceInfo> GetNetworkInterfaces()
182194
}
183195

184196
/// <summary>
185-
/// Detects the local IP address from routing to a remote IP address asynchronously.
197+
/// Asynchronously determines the local IP address that would be used to route traffic to the specified remote IP
198+
/// address.
186199
/// </summary>
187-
/// <param name="remoteIPAddress">The remote IP address to check routing against.</param>
188-
/// <returns>A task that represents the asynchronous operation.
189-
/// The task result contains the local <see cref="IPAddress"/> used to reach the remote address or null on error.</returns>
200+
/// <remarks>This method is useful for identifying the local network interface that would be selected by
201+
/// the system's routing table when communicating with a given remote address. The result may vary depending on the
202+
/// current network configuration and routing rules.</remarks>
203+
/// <param name="remoteIPAddress">The destination IP address for which to determine the corresponding local source IP address. Cannot be null.</param>
204+
/// <returns>A task that represents the asynchronous operation. The task result contains the local IP address that would be
205+
/// used to reach the specified remote IP address.</returns>
190206
public static Task<IPAddress> DetectLocalIPAddressBasedOnRoutingAsync(IPAddress remoteIPAddress)
191207
{
192208
return Task.Run(() => DetectLocalIPAddressFromRouting(remoteIPAddress));
193209
}
194210

195211
/// <summary>
196-
/// Detects the local IP address from routing to a remote IP address.
212+
/// Determines the local IP address that would be used to route traffic to the specified remote IP address.
197213
/// </summary>
198-
/// <param name="remoteIPAddress">The remote IP address to check routing against.</param>
199-
/// <returns>The local <see cref="IPAddress"/> used to reach the remote address or null on error.</returns>
214+
/// <remarks>This method creates a UDP socket to determine the local IP address selected by the system's
215+
/// routing table for the given remote address. No data is sent over the network. This method may return null if the
216+
/// routing information is unavailable or an error occurs.</remarks>
217+
/// <param name="remoteIPAddress">The destination IP address for which to determine the local routing address. Must not be null.</param>
218+
/// <returns>An IPAddress representing the local address that would be used to reach the specified remote address; or null if
219+
/// the local address cannot be determined.</returns>
200220
private static IPAddress DetectLocalIPAddressFromRouting(IPAddress remoteIPAddress)
201221
{
202222
var isIPv4 = remoteIPAddress.AddressFamily == AddressFamily.InterNetwork;
@@ -296,21 +316,26 @@ public static IPAddress DetectLocalIPAddressFromNetworkInterface(AddressFamily a
296316
}
297317

298318
/// <summary>
299-
/// Detects the gateway IP address from a local IP address asynchronously.
319+
/// Asynchronously detects the default gateway address associated with the specified local IP address.
300320
/// </summary>
301-
/// <param name="localIPAddress">The local IP address to find the gateway for.</param>
302-
/// <returns>A task that represents the asynchronous operation.
303-
/// The task result contains the gateway as <see cref="IPAddress"/> or null if not found.</returns>
321+
/// <param name="localIPAddress">The local IP address for which to determine the corresponding default gateway. Cannot be null.</param>
322+
/// <returns>A task that represents the asynchronous operation. The task result contains the IP address of the detected
323+
/// default gateway, or null if no gateway is found.</returns>
304324
public static Task<IPAddress> DetectGatewayFromLocalIPAddressAsync(IPAddress localIPAddress)
305325
{
306326
return Task.Run(() => DetectGatewayFromLocalIPAddress(localIPAddress));
307327
}
308328

309329
/// <summary>
310-
/// Detects the gateway IP address from a local IP address.
330+
/// Attempts to determine the default gateway address associated with the specified local IP address.
311331
/// </summary>
312-
/// <param name="localIPAddress">The local IP address to find the gateway for.</param>
313-
/// <returns>The gateway as <see cref="IPAddress"/> or null if not found.</returns>
332+
/// <remarks>This method searches all available network interfaces to find one that has the specified
333+
/// local IP address assigned. If found, it returns the first associated gateway address for that interface and
334+
/// address family. Returns null if the local IP address is not assigned to any interface or if no gateway is
335+
/// configured.</remarks>
336+
/// <param name="localIPAddress">The local IP address for which to detect the corresponding gateway. Must be either an IPv4 or IPv6 address.</param>
337+
/// <returns>An IPAddress representing the default gateway for the specified local IP address, or null if no matching gateway
338+
/// is found.</returns>
314339
private static IPAddress DetectGatewayFromLocalIPAddress(IPAddress localIPAddress)
315340
{
316341
foreach (var networkInterface in GetNetworkInterfaces())
@@ -334,19 +359,23 @@ private static IPAddress DetectGatewayFromLocalIPAddress(IPAddress localIPAddres
334359
}
335360

336361
/// <summary>
337-
/// Configures a network interface with the specified configuration asynchronously.
362+
/// Asynchronously applies the specified network interface configuration.
338363
/// </summary>
339-
/// <param name="config">The configuration to apply.</param>
364+
/// <param name="config">The configuration settings to apply to the network interface. Cannot be null.</param>
340365
/// <returns>A task that represents the asynchronous operation.</returns>
341366
public Task ConfigureNetworkInterfaceAsync(NetworkInterfaceConfig config)
342367
{
343368
return Task.Run(() => ConfigureNetworkInterface(config));
344369
}
345370

346371
/// <summary>
347-
/// Configures a network interface with the specified configuration.
372+
/// Configures the network interface according to the specified settings.
348373
/// </summary>
349-
/// <param name="config">The configuration to apply.</param>
374+
/// <remarks>This method applies the provided network configuration by executing system commands. If
375+
/// static IP or DNS settings are enabled in the configuration, the corresponding values are set; otherwise, DHCP is
376+
/// used. The method may prompt for elevated permissions depending on system policy.</remarks>
377+
/// <param name="config">An object containing the configuration parameters for the network interface, including IP address, subnet mask,
378+
/// gateway, and DNS server settings. Cannot be null.</param>
350379
private void ConfigureNetworkInterface(NetworkInterfaceConfig config)
351380
{
352381
// IP
@@ -382,17 +411,21 @@ private void ConfigureNetworkInterface(NetworkInterfaceConfig config)
382411
}
383412

384413
/// <summary>
385-
/// Flush the DNS cache asynchronously.
414+
/// Asynchronously flushes the system DNS resolver cache.
386415
/// </summary>
387-
/// <returns>Running task.</returns>
416+
/// <remarks>This method initiates the DNS cache flush operation on a background thread. The operation may
417+
/// require elevated permissions depending on the system configuration.</remarks>
418+
/// <returns>A task that represents the asynchronous flush operation.</returns>
388419
public static Task FlushDnsAsync()
389420
{
390421
return Task.Run(FlushDns);
391422
}
392423

393424
/// <summary>
394-
/// Flush the DNS cache.
425+
/// Clears the local DNS resolver cache on the system by executing the appropriate system command.
395426
/// </summary>
427+
/// <remarks>This method requires administrative privileges to successfully flush the DNS cache. If the
428+
/// application does not have sufficient permissions, the operation may fail.</remarks>
396429
private static void FlushDns()
397430
{
398431
const string command = "ipconfig /flushdns;";
@@ -401,21 +434,25 @@ private static void FlushDns()
401434
}
402435

403436
/// <summary>
404-
/// Release or renew the IP address of the specified network adapter asynchronously.
437+
/// Asynchronously releases and renews the IP configuration for the specified network adapter using the given mode.
405438
/// </summary>
406-
/// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param>
407-
/// <param name="adapterName">Name of the ethernet adapter.</param>
408-
/// <returns>Running task.</returns>
439+
/// <param name="mode">The release and renew operation mode to apply to the network adapter.</param>
440+
/// <param name="adapterName">The name of the network adapter whose IP configuration will be released and renewed. Cannot be null or empty.</param>
441+
/// <returns>A task that represents the asynchronous release and renew operation.</returns>
409442
public static Task ReleaseRenewAsync(IPConfigReleaseRenewMode mode, string adapterName)
410443
{
411444
return Task.Run(() => ReleaseRenew(mode, adapterName));
412445
}
413446

414447
/// <summary>
415-
/// Release or renew the IP address of the specified network adapter.
448+
/// Releases and/or renews the IP configuration for the specified network adapter using the given mode.
416449
/// </summary>
417-
/// <param name="mode">ipconfig.exe modes which are used like /release(6) or /renew(6)</param>
418-
/// <param name="adapterName">Name of the ethernet adapter.</param>
450+
/// <remarks>This method executes the appropriate 'ipconfig' commands based on the specified mode. The
451+
/// operation affects only the adapter identified by the provided name. Ensure that the caller has sufficient
452+
/// privileges to modify network settings.</remarks>
453+
/// <param name="mode">A value that specifies which IP configuration operation to perform. Determines whether to release, renew, or
454+
/// perform both actions for IPv4 and/or IPv6 addresses.</param>
455+
/// <param name="adapterName">The name of the network adapter to target for the release or renew operation. Cannot be null or empty.</param>
419456
private static void ReleaseRenew(IPConfigReleaseRenewMode mode, string adapterName)
420457
{
421458
var command = string.Empty;
@@ -436,19 +473,23 @@ private static void ReleaseRenew(IPConfigReleaseRenewMode mode, string adapterNa
436473
}
437474

438475
/// <summary>
439-
/// Add an IP address to a network interface asynchronously.
476+
/// Asynchronously adds an IP address to the specified network interface using the provided configuration.
440477
/// </summary>
441-
/// <param name="config">Ethernet adapter name, IP address and subnetmask.</param>
442-
/// <returns>Running task.</returns>
478+
/// <param name="config">The configuration settings that specify the network interface and IP address to add. Cannot be null.</param>
479+
/// <returns>A task that represents the asynchronous operation.</returns>
443480
public static Task AddIPAddressToNetworkInterfaceAsync(NetworkInterfaceConfig config)
444481
{
445482
return Task.Run(() => AddIPAddressToNetworkInterface(config));
446483
}
447484

448485
/// <summary>
449-
/// Add an IP address to a network interface.
486+
/// Adds an IP address to the specified network interface using the provided configuration.
450487
/// </summary>
451-
/// <param name="config">Ethernet adapter name, IP address and subnetmask.</param>
488+
/// <remarks>If DHCP/static IP coexistence is enabled in the configuration, the method enables this
489+
/// feature before adding the IP address. This method requires appropriate system permissions to modify network
490+
/// interface settings.</remarks>
491+
/// <param name="config">The network interface configuration containing the interface name, IP address, subnet mask, and DHCP/static
492+
/// coexistence settings. Cannot be null.</param>
452493
private static void AddIPAddressToNetworkInterface(NetworkInterfaceConfig config)
453494
{
454495
var command = string.Empty;
@@ -462,19 +503,23 @@ private static void AddIPAddressToNetworkInterface(NetworkInterfaceConfig config
462503
}
463504

464505
/// <summary>
465-
/// Remove an IP address from a network interface asynchronously.
506+
/// Asynchronously removes the IP address specified in the configuration from the associated network interface.
466507
/// </summary>
467-
/// <param name="config">Ethernet adapter name, IP address</param>
468-
/// <returns>Running task.</returns>
508+
/// <param name="config">The configuration object that specifies the network interface and IP address to remove. Cannot be null.</param>
509+
/// <returns>A task that represents the asynchronous remove operation.</returns>
469510
public static Task RemoveIPAddressFromNetworkInterfaceAsync(NetworkInterfaceConfig config)
470511
{
471512
return Task.Run(() => RemoveIPAddressFromNetworkInterface(config));
472513
}
473514

474515
/// <summary>
475-
/// Remove an IP address from a network interface.
516+
/// Removes the specified IP address from the given network interface configuration.
476517
/// </summary>
477-
/// <param name="config">Ethernet adapter name, IP address</param>
518+
/// <remarks>This method removes the IP address from the network interface using a system command. The
519+
/// operation requires appropriate system permissions and may fail if the interface or IP address does not
520+
/// exist.</remarks>
521+
/// <param name="config">The network interface configuration containing the name of the interface and the IP address to remove. Cannot be
522+
/// null.</param>
478523
private static void RemoveIPAddressFromNetworkInterface(NetworkInterfaceConfig config)
479524
{
480525
var command = $"netsh interface ipv4 delete address '{config.Name}' {config.IPAddress};";
@@ -488,8 +533,8 @@ private static void RemoveIPAddressFromNetworkInterface(NetworkInterfaceConfig c
488533
#region Events
489534

490535
/// <summary>
491-
/// Occurs when the user has canceled an operation (e.g. UAC prompt).
492-
/// </summary>
536+
/// Occurs when the user cancels the current operation (e.g. UAC prompt).
537+
/// </summary>
493538
public event EventHandler UserHasCanceled;
494539

495540
private void OnUserHasCanceled()

0 commit comments

Comments
 (0)