GetAdaptersAddresses in C#

  • Thread starter Thread starter James Jenkins
  • Start date Start date
J

James Jenkins

Hi - Does anyone know what GetAdaptersAddresses function looks like in C#. I
am having problems converting from C++ - thanks

James
 
Hi - Does anyone know what GetAdaptersAddresses function looks like in C#.
I am having problems converting from C++ - thanks

Hi James,

there don't seem to be existing definitions on the net, so use WMI, it is
far easier.

The following code returns an Array of Strings with the IP-addresses for
_all_ adapters on the local machine.

//add a reference to System.Management
using System.Management;
private string[] GetIPAddresses()
{
string[] addresses = null;
try
{
ArrayList Temp = new ArrayList();
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration ") ;
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection )
{
if ((bool)mo["IpEnabled"])
{
string[] ips = (string[])mo["IPAddress"];
foreach (string s in ips)
{
Temp.Add (s);
}
}
}

if (Temp.Count > 0)
{
addresses = new string[Temp.Count];
Temp.CopyTo (addresses);
}
else
{
addresses = new string[0];
}
}
catch (Exception) {}
return addresses;
}


Cheers

Arne Janning
 
Arne Janning said:
Hi - Does anyone know what GetAdaptersAddresses function looks like in
C#. I am having problems converting from C++ - thanks

Hi James,

there don't seem to be existing definitions on the net, so use WMI, it is
far easier.

The following code returns an Array of Strings with the IP-addresses for
_all_ adapters on the local machine.

//add a reference to System.Management
using System.Management;
private string[] GetIPAddresses()
{
string[] addresses = null;
try
{
ArrayList Temp = new ArrayList();
ManagementObjectSearcher query = new ManagementObjectSearcher(
"SELECT * FROM Win32_NetworkAdapterConfiguration ") ;
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection )
{
if ((bool)mo["IpEnabled"])
{
string[] ips = (string[])mo["IPAddress"];
foreach (string s in ips)
{
Temp.Add (s);
}
}
}

if (Temp.Count > 0)
{
addresses = new string[Temp.Count];
Temp.CopyTo (addresses);
}
else
{
addresses = new string[0];
}
}
catch (Exception) {}
return addresses;
}


Cheers

Arne Janning


Thanks - I had come to the conclusion that unless i recreate the function,
then I would have to use C++ - a learning curve too much at present. The wmi
seems like a good option - thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top