I am working on an application that retrieves network information based on certain values in a WMI query. I may be going about this the wrong way but what I am trying to accomplish is this:
----- BEGIN -----
string query = "SELECT * FROM Win32_NetworkAdapterConfiguration"
+ " WHERE DNSDomain = ' " + domain + " ' ";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
lblMachineName.Text = mo["DNSHostName"].ToString();
lblMAC.Text = mo["MACAddress"].ToString();
lblDomain.Text = mo["DNSDomain"].ToString();
string addresses = (string)mo["IPAddress"];
foreach (string ipaddress in addresses)
lblIPAddress.Text = ipaddress;
string subnets = (string)mo["IPSubnet"];
foreach (string ipsubnet in subnets)
lblSubnet.Text = ipsubnet;
string defaultgateways = (string)mo["DefaultIPGateway"];
foreach (string defaultipgateway in defaultgateways)
lblGateway.Text = defaultipgateway;
---- END ----
I tried using "WHERE IPEnabled = 'TRUE'" but on systems with more than on network adapter enabled it causes the application to iterate through the various adapters and ends up crashing because of "object reference not set to an instance.. etc".
I want to be able to get the users domain name (ex. "domain.com") and assign the "domain" variable a value that is retrived from the users machine and not have to go back and change the code everytime i use it on a different domain. It works when i simply remove the variable from the query and use my domain or another domain. I also tried "Environment.UserDomainName" but all it returns is the netbios name which does not work either. Ultimately I want to be able to use this application across multiple domains on systems were there may be more than 1 network card but only one being using the domain.
Thanks for any help, this is very urgent and I am awarding 500 points or more if need be.