Getting IP adapters list

  • Thread starter Thread starter Nuno Magalhaes
  • Start date Start date
N

Nuno Magalhaes

In a sample code I do something like this:

//Populate IP List
IPHost=Dns.GetHostByName(Dns.GetHostName());
DefaultIPCombo.Items.Clear();
DefaultIPCombo.Items.Add(IPHost.AddressList[0].ToString());
DefaultIPCombo.SelectedIndex=0;

How can I get all the IP adapters since this does only give me the IP
address of the default adapter. I have a wireless connection also and
would like to populate the DefaultIPCombo with all my network IPs. Is
this possible under C# .NET? Changing the IP manually and using the
connection at a specific adapter is possible but now... how to get all
the IPs?

I don't get it.

Thank you very much.
 
Try,
using System.Management;
..
..
..
using (ManagementObjectSearcher managementObjectSearcher = new
ManagementObjectSearcher("SELECT * From Win32_NetworkAdapterConfiguration
WHERE IPEnabled = 1"))
{
using (ManagementObjectCollection managementObjectCollection =
managementObjectSearcher.Get())
{
foreach (ManagementObject managementObject in managementObjectCollection)
{
string desc = string.Empty, ipAddress = string.Empty, subnetMask =
string.Empty, macAddress = string.empty;

try
{
desc = managementObject[ "Description" ].ToString();
}
catch{}

try
{
ipAddress = ((string[])managementObject[ "IPAddress" ])[ 0 ];
}
catch{}

try
{
subnetMask = ((string[])managementObject[ "IPSubnet" ])[ 0 ];
}
catch{}

try
{
macAddress = managementObject[ "MacAddress" ].ToString();
}
catch{}

managementObject.Dispose();
}
}
}

All the Best,
Phil.
 
Thank you. It works like a charm now. I'm getting the list of all IPs
available and enabled correctly.

Phil said:
Try,
using System.Management;
.
.
.
using (ManagementObjectSearcher managementObjectSearcher = new
ManagementObjectSearcher("SELECT * From Win32_NetworkAdapterConfiguration
WHERE IPEnabled = 1"))
{
using (ManagementObjectCollection managementObjectCollection =
managementObjectSearcher.Get())
{
foreach (ManagementObject managementObject in managementObjectCollection)
{
string desc = string.Empty, ipAddress = string.Empty, subnetMask =
string.Empty, macAddress = string.empty;

try
{
desc = managementObject[ "Description" ].ToString();
}
catch{}

try
{
ipAddress = ((string[])managementObject[ "IPAddress" ])[ 0 ];
}
catch{}

try
{
subnetMask = ((string[])managementObject[ "IPSubnet" ])[ 0 ];
}
catch{}

try
{
macAddress = managementObject[ "MacAddress" ].ToString();
}
catch{}

managementObject.Dispose();
}
}
}

All the Best,
Phil.

Nuno Magalhaes said:
In a sample code I do something like this:

//Populate IP List
IPHost=Dns.GetHostByName(Dns.GetHostName());
DefaultIPCombo.Items.Clear();
DefaultIPCombo.Items.Add(IPHost.AddressList[0].ToString());
DefaultIPCombo.SelectedIndex=0;

How can I get all the IP adapters since this does only give me the IP
address of the default adapter. I have a wireless connection also and
would like to populate the DefaultIPCombo with all my network IPs. Is
this possible under C# .NET? Changing the IP manually and using the
connection at a specific adapter is possible but now... how to get all
the IPs?

I don't get it.

Thank you very much.
 
Back
Top