Click here to Skip to main content
15,886,832 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I am working on a project for work. I am trying to retrieve the IP Address and System Name, and output them to separate text files. My method of choice in retrieving this information is WMI. I have run my queries through the wbemtest utility and both queries return the correct information. However, when I attempt to run the queries in my C# program, it returns blank lines. (I am returning output to the console in an attempt to test successful queries before adding file output functionality).

Here is the code I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Management;


namespace SysInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            string ipaddress = null; //IP Address retrived by query will be stored here to be output to a file
            string hostname = null; //Host Name retrieved by query will be stored here to be output to a file

            SelectQuery sQuery1 = new SelectQuery("SELECT IPAddress FROM Win32_NetworkAdapterConfiguration WHERE Description LIKE 'BROAD%'");
            ManagementObjectSearcher ipsearcher = new ManagementObjectSearcher(sQuery1);
            ManagementObjectCollection ipresults = ipsearcher.Get();


            foreach (ManagementObject ip in ipresults)
            {
                Console.WriteLine(ip.ToString());
            }

            SelectQuery sQuery2 = new SelectQuery ("SELECT SystemName FROM Win32_NetworkAdapter WHERE Description LIKE 'BROAD%'");
            ManagementObjectSearcher hostsearcher = new ManagementObjectSearcher(sQuery2);

            foreach (ManagementObject host in hostsearcher.Get())
            {
                Console.WriteLine(host.ToString());
            }

        }
    }
}


I am not utilizing OO-Design for this program, since it is a small program and will never be expanded in the future. Plus, I'm not a big fan of OOD, but I digress.

One thing I am finding, is that if I alter my query to 'SELECT *' rather than 'IPAddress' and 'SystemName', it does return the DeviceID of the network adapter. Is there a restriction on the SelectQuery function that it will only execute queries that retrieve all? There is nothing in Microsoft's documentation that supports this assumption.

Any help would be greatly appreciated.
Posted

public void UseWMI()
{
   string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" 
        + " WHERE IPEnabled = 'TRUE'";

   ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
   ManagementObjectCollection moCollection = moSearch.Get();
    
   foreach(ManagementObject mo in moCollection)
   {
      Console.WriteLine("HostName = " + mo["DNSHostName"]);
      Console.WriteLine("Description = " + mo["Description"]);
      

      string[] addresses = (string[])mo["IPAddress"];
      foreach(string ipaddress in addresses)
      {
         Console.WriteLine("IPAddress = " + ipaddress);
      }
      
      string[] subnets = (string[])mo["IPSubnet"];
      foreach(string ipsubnet in subnets)
      {
         Console.WriteLine("IPSubnet = " + ipsubnet);
      }
      

      string[] defaultgateways = (string[])mo["DefaultIPGateway"];
      foreach(string defaultipgateway in defaultgateways)
      {
         Console.WriteLine("DefaultIPGateway = " + defaultipgateway);
      }
   }
}
 
Share this answer
 
I know this is an old topic, but I wanted to post the final version of my code. Special thanks to anvas kuttan[^][ for his input - it was greatly appreciated! This was also the first real C# program that I've written (outside of Hello World!). I'm not a good programmer by any means (no matter what my BS in Computer Science says LOL). I really like C# and hope to expand my knowledge of it.

C#
using System;
using System.IO;
using System.Management;


namespace SysInfo
{
    class Program
    {
        static void Main(string[] args)
        {
            string ipaddress = null; //IP Address retrived by query will be stored here to be output to a file
            string hostname = null; //Host Name retrieved by query will be stored here to be output to a file

            StreamWriter hostwrite = new StreamWriter(@"c:\hostname.txt ", false);
            StreamWriter ipwrite = new StreamWriter(@"c:\ipaddress.txt", false);

            string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" + " WHERE Description LIKE 'BROAD%'";

            ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
            ManagementObjectCollection moCollection = moSearch.Get();

            foreach (ManagementObject mo in moCollection)
            {
                hostname = mo["DNSHostName"].ToString();
                hostwrite.WriteLine(hostname);


                string[] addresses = (string[])mo["IPAddress"];
                foreach (string ip in addresses)
                {
                    ipaddress = ip;
                    ipwrite.WriteLine(ipaddress);

                }
            }

            Console.WriteLine("If you see this window, you can close it");

            hostwrite.Close();
            ipwrite.Close();

        }
    }
}
 
Share this answer
 

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