使用WMI獲得硬盤的信息
2024-07-21 02:17:15
供稿:網友
首先,什么是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
注冊會員,創建你的web開發資料庫,