我正在玩WMI。我正在尝试修改下面的代码来修复一个错误,即它最初是由模型查询的。如果你有两个相同型号的驱动器,它只会返回第一个磁盘的\\。\ PHYSICALDEVICE。相反,我现在向用户呈现一个\\ PHYSICALDEVICE:MODEL字符串。他们从下拉列表中选择此项,我解析出物理设备并将其用于查询。
除了我在MOS查询中返回错误“无效查询”
之外 "SELECT * FROM Win32_DiskDrive WHERE DeviceID = '" + device + "'"
查询对我来说很好看,但显然不是。帮助赞赏。
完整代码:
private void Form1_Load(object sender, EventArgs e)
{
//Select item from drop down menu
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher(
"SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject moDisk in mosDisks.Get())
{
string devid = moDisk["Model"].ToString();
string drvnum = moDisk["DeviceID"].ToString();
cmbHdd.Items.Add(drvnum + ": " + devid);
}
}
private void cmbHdd_SelectedIndexChanged(object sender, EventArgs e)
{
//update form with selected device
int i = cmbHdd.SelectedItem.ToString().IndexOf(':');
string device = cmbHdd.SelectedItem.ToString();//.Substring(0,i);
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher(
"SELECT * FROM Win32_DiskDrive WHERE DeviceID = '" + device + "'");
foreach (ManagementObject moDisk in mosDisks.Get())
{
lblDeviceID.Text = "DeviceID: " + moDisk["DeviceID"].ToString();
}
}
答案 0 :(得分:3)
WMI要求在查询中将反斜杠(\
)加倍。
将\
替换为\\
变量中的device
,然后将其插入查询,看看是否有帮助。