Connecting Tech Pros Worldwide Help | Site Map

GetAdaptersAddresses in C#

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2005, 11:17 AM
James Jenkins
Guest
 
Posts: n/a
Default GetAdaptersAddresses in C#

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, 11:18 AM
Arne Janning
Guest
 
Posts: n/a
Default 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, 11:18 AM
James Jenkins
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.