.NET使用WMI获得硬盘的信息
2008-01-05 10:49:37 来源:WEB开发网核心提示:什么是WMI?WMI(Windows治理架构:Windows Management Instrumentation)是Microsoft基于Web的企业治理(WBEM)和 Desktop Management Task Force(DMTF)工业标准的实现. 就是一种基于标准的系统治理的开发接口,这组接口用来控制治理计
什么是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
更多精彩
赞助商链接