硬盘
首先,什么是WMI?
WMI(Windows管理架构:Windows Management Instrumentation)是Microsoft基于Web的企业管理(WBEM)和 Desktop Management Task Force(DMTF)工业标准的实现. 就是一种基于标准的系统管理的开发接口,这组接口用来控制管理计算机. 它提供了一种简单的方法来管理和控制系统资源.
如果你想深入了解他,可以参考Micorosft Platform SDK . 在这我们只是通过它实现一个简单的功能, 得到我们系统中硬盘的相关信息.
我们需要使用.net Framwork里面System.Management名字空间下提供的类来实现.
using System;
using System.Management;
using System.Collections;
using System.Collections.Specialized;
namespace ACE_Console
{
class ACE_Console
{
[STAThread]
static void Main(string[] args)
{
StringCollection propNames = new StringCollection();
ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
PropertyDataCollection props = driveClass.Properties;
foreach (PropertyData driveProperty in props)
{
propNames.Add(driveProperty.Name);
}
int idx = 0;
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drv in drives)
{
Console.WriteLine(" Drive({0}) Properties ", idx+1);
foreach (string strProp in propNames)
{
Console.WriteLine("Property: {0}, Value: {1}", strProp, drv[strProp]);
}
}
}
}
}
.net Framework SDK自带的帮助里有获得逻辑硬盘大小的代码:
[C#]
using System;
using System.Management;
// This example demonstrates getting information about a class using the ManagementClass object
class Sample_ManagementClass
{
public static int Main(string[] args)
{
ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");
diskClass.Get();
Console.WriteLine("Logical Disk class has " + diskClass.Properties.Count + " properties");
return 0;
}
}
[vb]
Imports System
Imports System.Management
// This example demonstrates getting information about a class using the ManagementClass
Class Sample_ManagementClass
Overloads Public Shared Function Main(args() As String) As Integer
Dim diskClass As New ManagementClass("Win32_LogicalDisk")
diskClass.Get()
Console.WriteLine(("Logical Disk class has " & diskClass.Properties.Count.ToString() & " properties"))
Return 0
End Function
End Class