| 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 |