C#开发 WMI

准备进行Hyper-v WMI的开发,在网上找到了比较实用的英文教程,在这里简单翻一下,原文地址是http://www.codeproject.com/KB/server-management/Intro2WMI.aspx#

创建一个客户端程序, 该程序通过WMI查询系统的逻辑磁盘的属性

WMI类可以被实例化,通过WMI查询语句可以查询对象。WMI查询语句可以像SQL查询语句一样使用,这里有详细的步骤来说明如何查询实例化的WMI对象。如果我们要使用Win32_LogicalDisks这个类,首先我们要熟悉这个类,熟悉这个类暴露的方法和属性。Win32_LogicalDisks这个类是从CIM_LogicalDisk 继承的,下面内容显示了再MSDN中Win32_LogicalDisks是如何定义的,清楚了解你的程序需要用到这个类的那些内容是非常重要的。

class Win32_LogicalDisk : CIM_LogicalDisk
{
  uint16   Access;
  uint16   Availability;
  uint64   BlockSize;
  string   Caption;
  boolean  Compressed;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  string   Description;
  string   DeviceID;
  uint32   DriveType;
  boolean  ErrorCleared;
  string   ErrorDescription;
  string   ErrorMethodology;
  string   FileSystem;
  uint64   FreeSpace;
  datetime InstallDate;
  uint32   LastErrorCode;
  uint32   MaximumComponentLength;
  uint32   MediaType;
  string   Name;
  uint64   NumberOfBlocks;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  string   ProviderName;
  string   Purpose;
  boolean  QuotasDisabled;
  boolean  QuotasIncomplete;
  boolean  QuotasRebuilding;
  uint64   Size;
  string   Status;
  uint16   StatusInfo;
  boolean  SupportsDiskQuotas;
  boolean  SupportsFileBasedCompression;
  string   SystemCreationClassName;
  string   SystemName;
  boolean  VolumeDirty;
  string   VolumeName;
  string   VolumeSerialNumber;
};

MSDN上有非常好的关于所有WMI类的文档,问题是我怎么知道在一个系统里有那些类?这很简单, ...,更好的选择是去使用一个公共的免费的资源,例如CIM Browser.

这里我要详细说下当你使用WMI类时你必须要使用的标准步骤。一旦你了解了这些步骤,你将会使用其他WMI类,因为步骤都是一样的。

步骤1:

导入引用System.Management

using System.Management;

下面的类是经常用到的

ConnectionOptions
ManagementScope
ObjectQuery
ManagementObjectSearcher
ManagementObjectCollection
ManagementObject

步骤2:

如果要连接远端服务器,创建一个ConnectionOptions对象,并给Username和Password赋值,

ConnectionOptions oConnect = new ConnectionOptions();
oConnect.Username = "CodeProject";
oConnect.Password = "Welcome";

步骤3:

我们需要设置一个命名空间,这个命名空间指出了我们将在哪里执行我们的操作,同时也是WMI类和CIM类注册的路径(这地方不是很清楚什么意思)。所以接下来的操作将在这个命名空间的背景下执行。下面代码显示了如何创建和设置management scope,\Root\CimV2 是在Win32_LogicalDisk 里定义的命名空间的路径。

System.Management.ManagementScope oMgmtScope = new System.Management.ManagementScope("\\root\\cimv2",oConnect );

步骤4:

写WMI的查询语句。

System.Management.ObjectQuery oQuery = new
                System.Management.ObjectQuery("select DeviceID, DriveType, FileSystem, FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");

步骤5:

执行了查询语句

ManagementObjectSearcher oSearch = new ManagementObjectSearcher(oMgmtScope,oQuery);

步骤6:

我们执行了查询语句之后,还需要将结果保存到 ManagementCollection对象中,然后可以循环这个对象并且获得想要的值。

ManagementObjectCollection oCollection = oSearch.Get();

步骤7:

循环ManagementCollection,并取值

//loop through all the logical drives and write out
//the details for the attributes requested
foreach( ManagementObject oReturnCollection in oCollection )
{   
DeviceID, DriveType, FileSystem
    //DeviceID
     Console.WriteLine("Device ID : " + oReturnCollection["DeviceID"].ToString());
     //DriveType
     Console.WriteLine("Drive Type : " + oReturnCollection["DriveType"].ToString());  
  
     //FileSystem
     Console.WriteLine("File System : " + oReturnCollection["FileSystem"].ToString());
    // Disk name
    Console.WriteLine("Name : " + oReturnCollection["Name"].ToString());
    // Free Space in bytes
    Console.WriteLine("FreeSpace: " + oReturnCollection["FreeSpace"].ToString());
    // Size in bytes
    Console.WriteLine("Size: " + oReturnCollection["Size"].ToString());
}

上面就是对c#使用WMI的一个介绍。你可以看到,不管你用什么WMI类,你都可以执行上面的这些步骤。你需要知道的是你要连接的命名空间以及你要使用哪个类。...

猜你喜欢

转载自mingzhuer18.iteye.com/blog/1118673
wmi
今日推荐