Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Get the Default gateway address c#

[DllImport("iphlpapi.dll", CharSet = CharSet.Auto)]
private static extern int GetBestInterface(UInt32 destAddr, out UInt32 bestIfIndex);

public static IPAddress GetGatewayForDestination(IPAddress destinationAddress)
{
    UInt32 destaddr = BitConverter.ToUInt32(destinationAddress.GetAddressBytes(), 0);

    uint interfaceIndex;
    int result = GetBestInterface(destaddr, out interfaceIndex);
    if (result != 0)
        throw new Win32Exception(result);

    foreach (var ni in NetworkInterface.GetAllNetworkInterfaces())
    {
        var niprops = ni.GetIPProperties();
        if (niprops == null)
            continue;

        var gateway = niprops.GatewayAddresses?.FirstOrDefault()?.Address;
        if (gateway == null)
            continue;

        if (ni.Supports(NetworkInterfaceComponent.IPv4))
        {
            var v4props = niprops.GetIPv4Properties();
            if (v4props == null)
                continue;

            if (v4props.Index == interfaceIndex)
                return gateway;
        }

        if (ni.Supports(NetworkInterfaceComponent.IPv6))
        {
            var v6props = niprops.GetIPv6Properties();
            if (v6props == null)
                continue;

            if (v6props.Index == interfaceIndex)
                return gateway;
        }
    }

    return null;
}
Comment

Get the Default gateway address c#

public static IPAddress GetDefaultGateway()
{
    return NetworkInterface
        .GetAllNetworkInterfaces()
        .Where(n => n.OperationalStatus == OperationalStatus.Up)
        .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback)
        .SelectMany(n => n.GetIPProperties()?.GatewayAddresses)
        .Select(g => g?.Address)
        .Where(a => a != null)
         // .Where(a => a.AddressFamily == AddressFamily.InterNetwork)
         // .Where(a => Array.FindIndex(a.GetAddressBytes(), b => b != 0) >= 0)
        .FirstOrDefault();
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: C# remain space 
Csharp :: c# linq foreach example 
Csharp :: internet connection sharing 
Csharp :: c# show hidden window wpf 
Csharp :: spreate by captial char in c# 
Csharp :: System.InvalidOperationException: No owin.Environment item was found in the context. 
Csharp :: bitwise even or odd 
Csharp :: resharper render pages folder asp.net core 
Csharp :: how to create vg in aix 
Csharp :: how to make a methode accessible from all the forms c# 
Csharp :: save a string as file to drive appscript 
Csharp :: C#$ 
Csharp :: xamarin xaml viewmodel 
Csharp :: Cursor Invisibility 
Csharp :: how to write an if statement with two checkboxes in c# 
Csharp :: Avoid auto-filling persian time picker 
Csharp :: dynamic c# .add 
Csharp :: ow-to-return-http-500-from-asp-net-core-rc2-web-api 
Csharp :: postgres .net 6 datetime issue 
Csharp :: c# dubble comment 
Csharp :: vb.net array search 
Csharp :: c# null coalescing operator 
Csharp :: using c# 
Csharp :: c# loop back 
Csharp :: c# datatable current row 
Csharp :: Selecting item from listview in C# 
Csharp :: how do i repeat a button on visual studio code 
Csharp :: call action method on checkbox click asp.net mvc without pageload 
Html :: marquee speed 
Html :: accept only image input file 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =