【发布时间】:2018-07-04 15:54:18
【问题描述】:
我需要识别所有驱动器和每个驱动器的所有逻辑分区,我在处理存储卡(笔记本电脑中的集成读卡器)时遇到了麻烦。
我正在使用此代码
void GetFullInfoDiskDrives()
{
Global.LogMessage("************** GetFullInfoDiskDrives() Getting complete info from all disk drives ", 1);
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject info in searcher.Get())
{
// Lets print all the properties per disk
PropertyDataCollection properties = info.Properties;
// display the properties
Global.LogMessage("----------------- Properties for the disk " + info["Model"].ToString(),1);
foreach (PropertyData property in properties)
{
Global.LogMessage("[" + property.Name + "] = '" + property.Value +"'", 1);
foreach (QualifierData q in property.Qualifiers)
{
Global.LogMessage(" -" + q.Name + "- = '" + q.Value + "'", 1);
}
}
string s = "";
string s_model = info["Model"].ToString();
string s_interface = (info["InterfaceType"]==null? info["MediaType"].ToString():info["InterfaceType"].ToString());
string s_serialnumber = (info["SerialNumber"] == null ? "No serial number": info["SerialNumber"].ToString());
Global.LogMessage("Disk detected (Model: " + s_model + " | Interface: " + s_interface + " | Serial#: " + s_serialnumber,1);
foreach (ManagementObject o in info.GetRelated("Win32_DiskPartition"))
foreach (ManagementObject i in o.GetRelated("Win32_LogicalDisk"))
s += i["Name"] + " ";
Global.LogMessage("Logical disks: " + s,1);
}
}
存储卡似乎没有 InterfaceType 的值,所以我需要改为读取 MediaType。序列号也是如此,我不知道在哪里可以找到存储卡。
此外,许多值在 value 属性中显示其类型(值),例如
[功能] = 'System.UInt16[]' -CIMTYPE- = 'uint16'
我怎样才能得到这个属性的真正价值?
我承认我有点迷茫,深入研究官方文档并不是一件容易的事...:/
有人可以点亮吗?
谢谢! 何塞
【问题讨论】:
-
“具有不正确的值类型”。
Capabilities是 一个UInt16[]数组。默认的ToString()方法返回类型名称,这就是你看到的。您可以将该属性转换为UInt16[]并打印出数组中的值(如果有帮助并且您知道如何解释它们)。 -
我不使用 ToString() 来显示属性的值,我使用 property.value。使用它,我可以看到其他属性的正确信息,例如 [MediaType] = 'Removable Media' - CIMTYPE- = 'string'。对于某些属性,它对其他包含其类型的属性具有实际值。那是我真的不明白...
-
我不知道你的
LogMessage方法,但我敢肯定你在某些时候会做类似Console.WriteLine()或相关电话的事情。如果您传递非字符串参数,这些调用 do 调用ToString()。MediaType是一个枚举,因此它的值被正确打印出来。没有输出数组内容的默认方法,ToString()在该数组上被调用并输出System.UInt16[]。 -
啊,它不依赖于
LogMessage方法,你的连接字符串。当您执行string s = "blah " + arrayVariable;时,会在arrayVariable上调用ToString()。 -
你知道一种方法来打印任何类型的值吗?所以我在所有情况下都用一条简单的线打印?我正在根据 CIMTYPE 的值对 case 语句进行成像,它看起来太乏味但可行。在你看来,这是要走的路吗?
标签: c# properties disk