Click here to Skip to main content
15,910,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After spending a day or two wandering around the net and inside the archive at Microsoft, I am starting to think I am looking for the holy grail. I am writing a little application that is basically ipconfig.exe as a windows app. And I have most of it figured out, except for things like the lease info, which I would like to have for completeness if nothing else. The new app is in C#. Any signposts or thoughts would be welcome.

Rand Ruggles
[email removed]
Posted
Updated 9-Oct-12 10:54am
v2
Comments
Zoltán Zörgő 9-Oct-12 16:54pm    
Email removed in your interest. Don't expose yourself to spammers!

1 solution

You can access these data via the Win32_NetworkAdapterConfiguration[^] WMI class.
Here[^] is a short code sample how to access the class properties:
C#
//create a management scope object
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");

//create object searcher
ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(scope, query);

//get collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection) 
{
// access properties of the WMI object
  Console.WriteLine("ArpAlwaysSourceRoute : {0}", m["ArpAlwaysSourceRoute"]);
  
}
 
Share this answer
 
Comments
Rand Ruggles 9-Oct-12 17:42pm    
Hello Zoltan, and thank you on two fronts. I just tried the code you submitted, and it was behaving very slugishly, even for COM objects. And when it came back, it was basically reporting nothings as it seemed some of its functionality was timing out when I was walking through the object trees. I have used that code with a different query that worked as far as pulling up info on each of the adapters. Yet leasing info was not in the set for each NIC. Please realize my little app is designed to run on the client, and just interogate client resources if at all possible, not sitting on the server and sorting through everything up there.
Zoltán Zörgő 10-Oct-12 2:28am    
I have the tool for you to generate the wmi query code directly in C#, it is called WMIgen: http://www.robvanderwoude.com/wmigen.php
And an other one if you want to see what wmi is reporting: http://www.ks-soft.net/hostmon.eng/wmi/index.htm.
This is what the generator generated with just a click of a button:

// WMI query to list all properties and values of the root\CIMV2:Win32_NetworkAdapterConfiguration class.
// This C# code was generated using the WMI Code Generator, Version 7.03
// http://www.robvanderwoude.com/wmigen.php

using System;
using System.Linq;
using System.Management;

namespace WMIGen
{
public class Win32_NetworkAdapterConfiguration_Query
{
public static int Main(string[] args)
{
try
{
string computer = string.Empty;
if (args.Count() == 1)
{
computer = @"\\" + args[0] + @"\";
}

ManagementObjectSearcher searcher = new ManagementObjectSearcher(computer + @"root\CIMV2", "SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection colItems = searcher.Get( );

Console.WriteLine( "{0} instance{1}", colItems.Count, ( colItems.Count == 1 ? String.Empty : "s" ) );
Console.WriteLine( );

foreach ( ManagementObject queryObj in colItems )
{
Console.WriteLine("ArpAlwaysSourceRoute : {0}", queryObj["ArpAlwaysSourceRoute"]);
Console.WriteLine("ArpUseEtherSNAP : {0}", queryObj["ArpUseEtherSNAP"]);
Console.WriteLine("Caption : {0}", queryObj["Caption"]);
Console.WriteLine("DatabasePath : {0}", queryObj["DatabasePath"]);
Console.WriteLine("DeadGWDetectEnabled : {0}", queryObj["DeadGWDetectEnabled"]);
if (queryObj["DefaultIPGateway"] == null)
{
Console.WriteLine("DefaultIPGateway : {0}", queryObj["DefaultIPGateway"]);
}
else
{
string[] arrProperty = (string[])(queryObj["DefaultIPGateway"]);
Console.Write("DefaultIPGateway : ");
Console.WriteLine(string.Join(";", arrProperty.Select(cap => cap.ToString()).ToArray()));
}
Console.WriteLine("DefaultTOS : {0}", queryObj["DefaultTOS"]);
Console.WriteLine("DefaultTTL : {0}", queryObj["DefaultTTL"]);
Console.WriteLine("Description : {0}", queryObj["Description"]);
Console.WriteLine("DHCPEnabled : {0}", queryObj["DHCPEnabled"]);
Console.WriteLine("DHCPLeaseExpires : {0}", queryObj["DHCPLeaseExpires"]);
Console.WriteLine("DHCPLeaseObtained : {0}", queryObj["DHCPLeaseObtained"]);
Console.WriteLine("DHCPServer : {0}", queryObj["DHCPServer"]);
Console.WriteLine("DNSDomain : {0}", queryObj["DNSDomain"]);
if (queryObj["DNSDomainSuffixSearchOrder"] == null)
{
Console.WriteLine("DNSDomainSuffixSearchOrder : {0}", queryObj["DNSDomainSuffixSearchOrder"]);
}
else
{
string[] arrProperty = (string[])(queryObj["DNSDomainSuffixSearchOrder"]);
Console.Write("DNSDomainSuffixSearchOrder : ");
Console.WriteLine(string.Join(";", arrProperty.Select(cap => cap.ToString()).ToArray()));
}
Console.WriteLine("DNSEnabledForWINSResolution : {0}", queryObj["DNSEnabledForWINSResolution"]);
Console.WriteLine("DNSHostName : {0}", queryObj["DNSHostName"]);
if (queryObj["DNSServerSearchOrder"] == null)
{
Console.WriteLine("DNSServerSearchOrder : {0}", queryObj["DNSServerSearchOrder"]);
}
else
{
string[] arrProperty = (string[])(queryObj["DNSServerSearchOrder"]);
Console.Write("DNSServerSearchOrder : ");
Console.WriteLine(string.Join(";", arrProperty.Select(cap => cap.ToString()).ToArray()));
}
Console.WriteLine("DomainDNSRegistrationEnabled : {0}", queryObj["DomainDNSRegistrationEnabled"]);
Console.WriteLine("ForwardBufferMemory : {0}
Rand Ruggles 10-Oct-12 10:32am    
Thank You Zoltan. I did some more digging last night and this morning. I am dev/testing this on my laptop to minimize the port count, and I was making a bad assumption that I would only have to iterate through a query result of about three adapters. The code is actually returning a query result of 23!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900