How to get the default connection name for NIC

IMPORTANT CHANGE: Back to this topic. As you said, there should be no default network adapter, I'm trying to figure out if there is a way to detect all connected network adapters.

Having the MAC address of my physical interface, there is a software way to get the status of an interface / interface / etc ...

For example, my XP machine:

Realtek RTL8139 Device PCI Fast Ethernet NIC MAC Family XXXX-XXXX-XXXX

XXXX-XXXX-XXXX is what I know

Using this device, I connect using the connection "Local Area Connection" (with all the information related to both the gateway, subnet, ...)

So, I am looking for a link between XXXX-XXXX-XXXX and a local connection.

I hope everything is clear now.

Thanks everyone! Postscript Sorry for the delay ... +1 vote for everyone, for patience!


Old question


Hi everyone, I would like to change the LAN Connection IP address using the netsh command.

My problem is the programmatic way to get the default connection name (i.e., "Local Area Connection")?

thanks

EDIT: I don't need a list of all connection names, but only by default. Access to the registry I get a list, and it seems that the default is *. Unfortunately, by printing them to the console, I get 10 different "local connections", such as ...

Local Area Connection* 13 6TO4 Adapter VMware Network Adapter VMnet1 Wireless Network Connection 2 Reusable ISATAP Interface {483968F2-DBF9-4596-B8BE-725FAAB89F93} Local Area Connection* 3 Local Area Connection* 2 Reusable Microsoft 6To4 Adapter Local Area Connection* 7 VMware Network Adapter VMnet8 Local Area Connection* 8 isatap.replynet.prv Local Area Connection* 9 Local Area Connection* 12 isatap.{FAA80CE0-D641-408A-83F8-5F9C394FFD76} Bluetooth Network Connection Local Area Connection* 4 isatap.{40156BF9-6599-4912-A315-62DE5342B452} isatap.{7651F2F5-4888-4258-92C5-6822C506D726} Local Area Connection* 5 isatap.{34F5F074-8AA7-4421-AE24-131BA2DC3458} Local Area Connection* Local Area Connection* 10 Local Area Connection Local Area Connection* 6 Wireless Network Connection 

etc.

EDIT2: @ ho1 by running your code, changing the FriendlyName value that does not exist with the name, you will get something like a list behind, unfortunately, it seems that this is not the expected result

 0 - WAN Miniport (SSTP) 1 - WAN Miniport (IKEv2) 2 - WAN Miniport (L2TP) 3 - WAN Miniport (PPTP) 4 - WAN Miniport (PPPOE) 5 - WAN Miniport (IPv6) 6 - WAN Miniport (Network Monitor) 7 - Realtek RTL8168C(P)/8111C(P) Family PCI-E Gigabit Ethernet NIC (NDIS 6.20) 8 - WAN Miniport (IP) 9 - Microsoft ISATAP Adapter 10 - RAS Async Adapter 11 - Broadcom 802.11g Network Adapter 12 - Microsoft 6to4 Adapter 13 - VMware Virtual Ethernet Adapter for VMnet1 14 - Microsoft ISATAP Adapter #3 15 - VMware Virtual Ethernet Adapter for VMnet8 16 - Microsoft ISATAP Adapter #2 17 - Microsoft ISATAP Adapter #4 18 - Microsoft Virtual WiFi Miniport Adapter 19 - Microsoft ISATAP Adapter #5 20 - Microsoft ISATAP Adapter 22 - Bluetooth Device (Personal Area Network) 23 - Microsoft 6to4 Adapter 24 - Microsoft 6to4 Adapter #3 25 - Microsoft 6to4 Adapter #2 
+4
source share
6 answers

This is a dirty way to do this, since it can be optimized by including 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 probably want to adapt the results using the results from nic.GetIPProperties() and nic.GetIPProperties().GetIPv4Properties()

+2
source

As mentioned, Windows does not have a default NIC adapter. The network adapter used is selected based on the target network (address) and metric.

For example, if you have two network cards 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 select Local Area Connection and route this way. Conversely, if you want to connect to 10.1.50.30 , Windows will select 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 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.

+5
source
 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); } 

Using:

 var nic = GetAllNetworkInterfaces(new[] { NetworkInterfaceType.Tunnel, NetworkInterfaceType.Loopback }); 
+2
source

You can use the Win32_NetworkAdapter WMI class to list all adapters and have an Index property, which can mean that it is 0 or 1, since Index is the default value, or one of the other properties can help find the default value.

Something like this might be:

Edit: Fixed broken code (this is at least more likely to work). But after what abatischev said, I think you might need to use Win32_NetworkAdapterConfiguration.IPConnectionMetric to search for 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; } 
+2
source

My girlfriend (Ciro DA) had the same problem. Playing a bit with C #, we seemed to find a way to skip virtual (not actually connected Ip): we looked for gateaway, discarding not only those who did not have it, but those who had the dummy (0.0. 0.0), On my machine, these last, where exactly two virtual Vm-Ware 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 (eg, 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()); } 
+1
source

You can get a list of them, but not by default (perhaps you can assume that this is the first entry).

 static void Main(string[] args) { foreach (var nc in NetworkInterface.GetAllNetworkInterfaces()) { Console.WriteLine(nc.Name); } Console.ReadLine(); } 
0
source

All Articles