Connecting Tech Pros Worldwide Help | Site Map

GetAdaptersAddresses in C#

  #1  
Old November 16th, 2005, 12:17 PM
James Jenkins
Guest
 
Posts: n/a
Hi - Does anyone know what GetAdaptersAddresses function looks like in C#. I
am having problems converting from C++ - thanks

James


  #2  
Old November 16th, 2005, 12:18 PM
Arne Janning
Guest
 
Posts: n/a

re: GetAdaptersAddresses in C#


"James Jenkins" schrieb[color=blue]
> Hi - Does anyone know what GetAdaptersAddresses function looks like in C#.
> I am having problems converting from C++ - thanks[/color]

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


  #3  
Old November 16th, 2005, 12:18 PM
James Jenkins
Guest
 
Posts: n/a

re: GetAdaptersAddresses in C#



"Arne Janning" <spam.me-here.arnolo@msn.com> wrote in message
news:%23QsM8ydoEHA.596@TK2MSFTNGP11.phx.gbl...[color=blue]
> "James Jenkins" schrieb[color=green]
>> Hi - Does anyone know what GetAdaptersAddresses function looks like in
>> C#. I am having problems converting from C++ - thanks[/color]
>
> 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
>[/color]


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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make a library with this? Leonardo D'Ippolito answers 5 November 16th, 2005 12:28 PM