C#调用WMI
using System.Management;
using ROOT.CIMV2.Win32;
ConnectionOptions options = new ConnectionOptions();
options.Username = "administrator ";
//设定用户的口令
options.Password = "123 ";
ManagementScope Conn = new ManagementScope( "\\\\192.168.0.2\\root\\cimv2 ", options);
// LogicalDisk dskD = new LogicalDisk(Conn, new ManagementPath( "win32_LogicalDisk.DeviceId=\ "d:\ " "));
this.Response.Write( "FileSystem: " + dskD.FileSystem + " </br> ");
this.Response.Write( "Size: " + dskD.Size + " </br> ");
this.Response.Write( "FreeSpace: " + dskD.FreeSpace + " </br> ");
this.Response.Write( "DriveType: " + dskD.DriveType + " </br> ");
这段代码是获取192.168.0.2的D盘信息,请问高手怎么可以获得机器上所有分区的信息,( "win32_LogicalDisk.DeviceId=\ "d:\ " ")不是使用指定盘的.
------解决方案--------------------试试:
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher( "Select * From Win32_LogicalDisk ");
foreach (ManagementObject mo in searcher.Get())
{
// mo[ "FileSystem "],文件系统,如“FAT32”
// mo[ "FreeSpace "],剩余空间,如“4554891264”(4G多)
// mo[ "Name "],卷标,如“C:”
// mo[ "Size "],大小,如“10476945408”(10G)
}
}
catch
{
}
参见
http://www.cnblogs.com/KissKnife/archive/2006/11/25/572281.html