IMPORTANT EDIT: Back again on this subject. As you said there should be no default NIC, I\'m trying to understand if there is a way to detect all the NICs t
As others have mentioned, there is no "Default" NIC adapter in Windows. The NIC used is chosen based on the destination network (address) and the metric.
For example, if you have two NICs and two different networks:
10.1.10.1 - Local Area Connection (metric 20)
10.1.50.1 - Local Area Connection 2 (metric 10)
And you want to connect to 10.1.10.15
, Windows will choose Local Area Connection
and route that way. Conversely, if you want to connect to 10.1.50.30
, Windows will choose Local Area Connection 2
.
Now, if you try to connect to 74.125.67.106
(google.com), Windows will choose Local Area Connection 2
because it has a lower metric value.
EDIT: Here is a great article explaining routing - http://www.windowsnetworking.com/articles_tutorials/Making-Sense-Windows-Routing-Tables.html
EDIT2: Spelling.
Hope this helps.
using System.Linq;
using System.Net.NetworkInformation;
var nic = NetworkInterface
.GetAllNetworkInterfaces()
.FirstOrDefault(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.NetworkInterfaceType != NetworkInterfaceType.Tunnel);
var name = nic.Name;
or more elegant solution:
.Where(i => !(
new[] { NetworkInterfaceType.Loopback, NetworkInterfaceType.Tunnel }
.Contains(i.NetworkInterfaceType)))
or if you want to practice in LINQ:
static IEnumerable<NetworkInterface> GetAllNetworkInterfaces(IEnumerable<NetworkInterfaceType> excludeTypes)
{
var all = NetworkInterface.GetAllNetworkInterfaces();
var exclude = all.Where(i => excludeTypes.Contains(i.NetworkInterfaceType));
return all.Except(exclude);
}
Usage:
var nic = GetAllNetworkInterfaces(new[] { NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback });
This is a dirty way of doing it as it can be optimized by incorporating LINQ, etc
using System.Net.NetworkInformation;
List<NetworkInterface> Interfaces = new List<NetworkInterface>();
foreach (var nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
Interfaces.Add(nic);
}
}
NetworkInterface result = null;
foreach (NetworkInterface nic in Interfaces)
{
if (result == null)
{
result = nic;
}
else
{
if (nic.GetIPProperties().GetIPv4Properties() != null)
{
if (nic.GetIPProperties().GetIPv4Properties().Index < result.GetIPProperties().GetIPv4Properties().Index)
result = nic;
}
}
}
Console.WriteLine(result.Name);
you'll probably want to tailor your results by using the results from nic.GetIPProperties()
and nic.GetIPProperties().GetIPv4Properties()
You can get a list of them, but not the default (perhaps you can assume it is the first entry).
static void Main(string[] args)
{
foreach (var nc in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine(nc.Name);
}
Console.ReadLine();
}
You can use the WMI class Win32_NetworkAdapter to enumerate all the adapters and it has an Index
property which might mean that the one with 0 or 1 as the Index
is the default one, or one of the other properties might help to find the default one.
Something like this maybe:
Edit: Fixed broken code (this is at least more likely to work). But following what abatishchev said I think you might need to use Win32_NetworkAdapterConfiguration.IPConnectionMetric
to find the default adapter...
ManagementClass mc = new ManagementClass("Win32_NetworkAdapter");
foreach (ManagementObject mo in mc.GetInstances())
{
int index = Convert.ToInt32(mo["Index"]);
string name = mo["NetConnectionID"] as string;
if (!string.IsNullOrEmpty(name))
textBox1.Text += name + Environment.NewLine;
}
A friend of mine (Ciro D.A.) had same problem. Playing around a bit with c#, we seemed to find a way to skip virtual (not really connected Ip): we looked for gateaway discarding not only those that did not have it, but also those who had a dummy (0.0.0.0) one. On my machine, these last where exactly two Vm-Ware virtual adapters.
public static void DisplayIPAddresses()
{
Console.WriteLine("\r\n****************************");
Console.WriteLine(" IPAddresses");
Console.WriteLine("****************************");
StringBuilder sb = new StringBuilder();
// Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface network in networkInterfaces)
{
if (network.OperationalStatus == OperationalStatus.Up )
{
if (network.NetworkInterfaceType == NetworkInterfaceType.Tunnel) continue;
//GatewayIPAddressInformationCollection GATE = network.GetIPProperties().GatewayAddresses;
// Read the IP configuration for each network
IPInterfaceProperties properties = network.GetIPProperties();
//discard those who do not have a real gateaway
if (properties.GatewayAddresses.Count > 0)
{
bool good = false;
foreach (GatewayIPAddressInformation gInfo in properties.GatewayAddresses)
{
//not a true gateaway (VmWare Lan)
if (!gInfo.Address.ToString().Equals("0.0.0.0"))
{
sb.AppendLine(" GATEAWAY "+ gInfo.Address.ToString());
good = true;
break;
}
}
if (!good)
{
continue;
}
}
else {
continue;
}
// Each network interface may have multiple IP addresses
foreach (IPAddressInformation address in properties.UnicastAddresses)
{
// We're only interested in IPv4 addresses for now
if (address.Address.AddressFamily != AddressFamily.InterNetwork) continue;
// Ignore loopback addresses (e.g., 127.0.0.1)
if (IPAddress.IsLoopback(address.Address)) continue;
if (!address.IsDnsEligible) continue;
if (address.IsTransient) continue;
sb.AppendLine(address.Address.ToString() + " (" + network.Name + ") nType:" + network.NetworkInterfaceType.ToString() );
}
}
}
Console.WriteLine(sb.ToString());
}