javirs javirs - 1 year ago 159
C# Question

differentiate VPN address and local address in C#

I managed to get all the NetworkInterface's in the computer, then I reject the ipv6, the loopback and then I end up having 2 interfaces, one is the real Ethernet I want to use and one is the VPN I have open to work with some remote servers that I don't want to use.

Is there any way in C# to identify them? Both answer "InterNetwork" for the AddressFamily and both say "Ethernet" for the NetworkingInterfaceType

Any clue ?

List<IPAddress> localIPs = new List<IPAddress>();

var ifaces = NetworkInterface.GetAllNetworkInterfaces();

//I'm working on unity and it does not allow me to do .ToList().. for some reason.
List<NetworkInterface> IfacesList = new List<NetworkInterface>();
foreach (var i in ifaces)

var selected = IfacesList.FindAll(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet);

if (selected.Count == 1)
var addresses = selected[0].GetIPProperties().UnicastAddresses;

//once again, cannot use .toList()
List<UnicastIPAddressInformation> addressesList = new List<UnicastIPAddressInformation>();
foreach (var address in addresses)

var ip = addressesList.Find(x => x.Address.AddressFamily == AddressFamily.InterNetwork).Address;
return new IPEndPoint(ip, port);
//HERE I need to differentiate my IP from VPN IP

those are the two interfaces I end up having:

enter image description here

Answer Source

You could find the interfaces which are attached to the PCI bus and go from there. Here's some code which only returns interfaces which are on the PCI bus.

using System.Collections.Generic;
using System.Linq;
using System.Management; // Add a reference to System.Management for the project
using System.Net.NetworkInformation;

namespace ConsoleApplication
    class Program
        static void Main(string[] args)
            HashSet<string> physicalMacAddresses = new HashSet<string>(GetMacAddressesOnPciBus());

            var ifaces = NetworkInterface.GetAllNetworkInterfaces();
            var physicalAddresses = ifaces
                .Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                && physicalMacAddresses.Contains(x.GetPhysicalAddress().ToString().Trim('{').Trim('}')));


        private static IEnumerable<string> GetMacAddressesOnPciBus()
            ManagementObjectSearcher searcher = new ManagementObjectSearcher
    ("Select MACAddress,PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL");
            ManagementObjectCollection mObject = searcher.Get();

            foreach (ManagementObject obj in mObject)
                string pnp = obj["PNPDeviceID"].ToString();
                if (pnp.Contains("PCI\\"))
                    string mac = obj["MACAddress"].ToString();
                    mac = mac.Replace(":", string.Empty);
                    yield return mac;

The technique for finding the MAC addresses is from here how-to-determine-mac-address-of-the-actual-physical-network-card-not-virtual.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download