Finding domain name for WMI Query

Posted on 2006-04-06
Last Modified: 2012-06-22
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. "") 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.
Question by:JoeDW
    1 Comment
    LVL 41

    Accepted Solution

    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.

    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()
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher("root\\CIMV2",
                        "SELECT * FROM Win32_ComputerSystem");

                    foreach (ManagementObject queryObj in searcher.Get())
                        Console.WriteLine("Win32_ComputerSystem instance");
                        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);

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now