我有以下代码通过物理磁盘并显示 WMI 设置。
我注意到,如果 WMI 参数不存在,它不会显示任何内容(这很好),但这会干扰服务器上存在的后续 WMI 参数,并且它们现在不显示任何内容......即使它们应该......下面的例子。
所以,我的意思是代码通过这部分:
lblcapability_desc.Text = "Capability Description: " + moDisk["CapabilityDescription"].ToString();
lblAvailability.Text = "Availability: " + moDisk["Availability"].ToString();
lblbytepersector.Text = "Bytes per Sector: " + moDisk["BytesPerSector"].ToString();
lbl_deviceid.Text = "Device ID: " + moDisk["systemname"].ToString();
它首先获取 SystemName,然后获取 Type,依此类推。
现在,如果在计算机上说“可用性”的 WMI 参数不存在,那么它将不会显示。那也行。但是,紧接在“可用性”之后有“每个扇区的字节数”参数和其他参数。这些参数也不显示任何内容(尽管我知道它们应该显示一些东西,因为它们确实存在于服务器上 - 通过 powershell 测试)。
好像“可用性”中缺少信息会干扰之后的参数并且不显示任何内容。
在解决这个问题时我能想到的唯一方法(这样即使可用性只显示后续参数显示他们需要的数据)是可能在每个 WMI 参数中添加一个 IF THEN - 不是很好的编码,但想不出解决此问题的一种方法。
所以一个例子是 IF [Availbility] 然后消息“Avialbility = 不管” ELSE “WMI 参数未找到”。这样,每个 WMI 参数都有自己的错误检查。我想不是很好的编码..
如果 WMI 参数不存在,则已捕获错误,然后向最终用户显示注释。
这是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using Microsoft.Win32;
namespace diskdrive_info
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//Get all the disk drives
ManagementObjectSearcher mosDisk = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
// Loop through each object (disk) retrieved by WMI
foreach (ManagementObject moDisk in mosDisk.Get())
{
cmbHdd.Items.Add(moDisk["Model"].ToString());
}
}
private void cmbHdd_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
foreach (ManagementObject moDisk in mosDisks.Get())
{
lblSystemName.Text = "SystemName: " + moDisk["systemname"];
lblType.Text = "Type: " + moDisk["MediaType"].ToString();
lblModel.Text = "Model: " + moDisk["Model"].ToString();
lblCapacity.Text = "Capacity: " + moDisk["Size"].ToString() + " bytes (" + Math.Round(((((double)Convert.ToDouble(moDisk["Size"]) / 1024) / 1024) / 1024), 2) + " GB)";
lblPartitions.Text = "Partitions: " + moDisk["Partitions"].ToString();
lblSectors.Text = "Sectors: " + moDisk["SectorsPerTrack"].ToString();
lblSignature.Text = "Signature: " + moDisk["Signature"].ToString();
lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();
lblFirmware.Text = "Firmware: " + moDisk["FirmwareRevision"] == null ? "Not Available" : moDisk["FirmwareRevision"].ToString();
lblcapability_desc.Text = "Capability Description: " + moDisk["CapabilityDescription"].ToString();
lblAvailability.Text = "Availability: " + moDisk["Availability"].ToString();
lblbytepersector.Text = "Bytes per Sector: " + moDisk["BytesPerSector"].ToString();
lbl_deviceid.Text = "Device ID: " + moDisk["systemname"].ToString();
}
}
catch (Exception exp)
{
lblError.Text = "Some properties were not shown due to WMI errors or member not available on this system";
}
}
private void btn_clear_Click(object sender, EventArgs e)
{
//lblSystemName.Text = string.Empty;
lblSystemName.Text = "SystemName: " + "";
lblType.Text = "Type: " + "";
lblModel.Text = "Model: " + "";
lblCapacity.Text = "Capacity: " + "";
lblPartitions.Text = "Partitions: " + "";
lblSectors.Text = "Sectors: " + "";
lblSignature.Text = "Signature: " + "";
lblFirmware.Text = "Firmware: " + "";
lblError.Text = String.Empty;
cmbHdd.Items.Clear();
cmbHdd.Text = "";
ManagementObjectSearcher mosDisk = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject moDisk in mosDisk.Get())
{
cmbHdd.Items.Add(moDisk["Model"].ToString());
}
}
}
}