争怎路由网:是一个主要分享无线路由器安装设置经验的网站,汇总WiFi常见问题的解决方法。

使用WMI获得硬盘的信息

时间:2024/7/16作者:未知来源:争怎路由网人气:

首先,什么是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

关键词:运用WMI获得硬盘的信息




Copyright © 2012-2018 争怎路由网(http://www.zhengzen.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版