Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my application programe, I need get the network adapter information to the user selection.
following is my code, the issue is if network enviornment is Wireless LAN IP 192.168.0.102,
Local Ethernet IP 192.168.0.106 for example. my code retrieve the information sequence for adapter is Wireless LAN, Local Ethernet, but for the IP sequence is 192.168.0.102, 192.168.0.106. but that is not match the real situation.

whether you can support me about this.


C#
foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
    if (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
    {    
        if (nic.OperationalStatus == OperationalStatus.Up)
            Adapter_LstB.Items.Add(nic.Name);
    }
    if (nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
    {
        if (nic.OperationalStatus == OperationalStatus.Up)
            Adapter_LstB.Items.Add(nic.Name);
    }
} 
foreach (IPAddress currrentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
    if (currrentIPAddress.AddressFamily.ToString() == System.Net.Sockets.AddressFamily.InterNetwork.ToString())
            IPAddress_LstB.Items.Add(currrentIPAddress.ToString());
        
}
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc) 
{
    if (mo["IPEnabled"].ToString() == "True")
        MACAddress_LstB.Items.Add(mo["MacAddress"].ToString());
}          
HostPC_IP.Text = IPAddress_LstB.Items[0].ToString();
Posted
Updated 7-Jul-14 20:13pm
v2

1 solution

Solve by myself

ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
{
Adapter_LstB.Items.Add(mo["Caption"].ToString().Substring(11));
string[] ipaddresses = (string[])mo["IPAddress"];
IPAddress_LstB.Items.Add(ipaddresses[0]);
MACAddress_LstB.Items.Add(mo["MacAddress"].ToString());
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900