Identifying active network interface

后端 未结 5 1824
走了就别回头了
走了就别回头了 2020-12-01 02:30

In a .NET application, how can I identify which network interface is used to communicate to a given IP address?

I am running on workstations with multiple network in

5条回答
  •  有刺的猬
    2020-12-01 02:50

    The info you are after will be in WMI.

    This example using WMI may get you most of the way:

    using System.Management;
    string query = "SELECT * FROM Win32_NetworkAdapterConfiguration";
    ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
    ManagementObjectCollection moCollection = moSearch.Get();// Every record in this collection is a network interface
    foreach (ManagementObject mo in moCollection)
    {    
        // Do what you need to here....
    }
    

    The Win32_NetworkAdapterConfiguration class will give you info about the configuration of your adapters e.g. ip addresses etc.

    You can also query the Win32_NetworkAdapter class to find out 'static'about each adapter (max speed, manufacturer etc)

提交回复
热议问题