使用C#查询WMI [英] Querying WMI Using C#

查看:66
本文介绍了使用C#查询WMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个工作项目.我正在尝试检索IP地址和系统名称,并将它们输出到单独的文本文件中.在检索此信息时,我选择的方法是WMI.我已经通过wbemtest实用程序运行了查询,并且两个查询都返回正确的信息.但是,当我尝试在C#程序中运行查询时,它返回空行. (我正在将输出返回到控制台,以尝试在添加文件输出功能之前测试成功的查询).

这是我到目前为止的代码:

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

        }
    }
}



我没有将OO-Design用于此程序,因为它是一个小程序,以后将永远不会扩展.另外,我不是OOD的忠实拥护者,但我离题了.

我发现的一件事是,如果将查询更改为"SELECT *"而不是"IPAddress"和"SystemName",它将返回网络适配器的DeviceID. SelectQuery函数是否有限制,使其仅执行检索全部内容的查询? Microsoft的文档中没有任何内容支持此假设.

任何帮助将不胜感激.



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.

推荐答案

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


我知道这是一个老话题,但是我想发布我的代码的最终版本.特别感谢 anvas kuttan [^] [
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.

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

        }
    }
}


这篇关于使用C#查询WMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆