Finding domain name for WMI Query

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();

            try
            {
                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.
LVL 1
JoeDWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grayeCommented:
I'd use the Win32_ComputerSystem object...  The domain property shows a fully formatted domain (on WinXP and above).  On Win2k, you'd need to poke around in the registry at HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters to get the Domain string value.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_computersystem.asp

Here is an example to demonstrate the concepts
using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT * FROM Win32_ComputerSystem");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_ComputerSystem instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Domain: {0}", queryObj["Domain"]);
                    Console.WriteLine("DomainRole: {0}", queryObj["DomainRole"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.