我怎么把异常捕获

时间:2012-09-21 14:29:43

标签: c# c#-4.0 wmi

我有以下代码通过并返回磁盘信息。运行时我注意到我的计算机上没有一些WMI属性(即FirmwareRevision)。

所以,当我运行代码时,VS正在崩溃,说明“未找到”。任何想法我如何在未找到状态“不可用”的对象的输出上放置异常捕获状态。

我一直在阅读异常捕获,但到目前为止我的业余方式是明确定义我想要使用的异常...这个程序打算在不同服务器的负载上运行,所以希望它可以智能地找出WMI对象任何一台服务器都没有。

代码如下,目前它失败(最后一行):

  

“lblFirmware.Text =”固件:“   + moDisk [ “FirmwareRevision”]的ToString();“

代码:

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)
        {
            ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE Model = '" + cmbHdd.SelectedItem + "'");
            foreach (ManagementObject moDisk in mosDisks.Get())
            {
                lblType.Text = "Type: " + moDisk["MediaType"].ToString();
                lblModel.Text = "Model: " + moDisk["Model"].ToString();
                lblCapacity.Text = "Capacity: " + moDisk["Size"].ToString();
                lblPartitions.Text = "Partitions: " + moDisk["Partitions"].ToString();
                lblSectors.Text = "Sectors: " + moDisk["SectorsPerTrack"].ToString();
                lblSignature.Text = "Signatures: " +moDisk["Signature"].ToString();
                lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();
            }
        }


    }
}

4 个答案:

答案 0 :(得分:3)

尝试并替换:

lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();

使用:

lblFirmware.Text = "Firmware: " + moDisk["FirmwareRevision"] == null ? "Not Available" :  moDisk["FirmwareRevision"].ToString();

答案 1 :(得分:2)

试试这个

private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            //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());
            }
        }
        catch(Exception exp)
        {

        }
    }

    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())
            {
                 lblType.Text = "Type: " + moDisk["MediaType"].ToString();
                 lblModel.Text = "Model: " + moDisk["Model"].ToString();
                 lblCapacity.Text = "Capacity: " + moDisk["Size"].ToString();
                 lblPartitions.Text = "Partitions: " + moDisk["Partitions"].ToString();
                 lblSectors.Text = "Sectors: " + moDisk["SectorsPerTrack"].ToString();
                 lblSignature.Text = "Signatures: " +moDisk["Signature"].ToString();
                 lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();
            }
        }
        catch(Exception exp)
        {

        }
    }

答案 2 :(得分:1)

这不是你问题的直接答案,但更好的答案是你不应该依赖try / catch。 Catch语句适用于您无法预见的情况,或者您的程序无法正常处理的情况。这些都不适用于此 - 您实际上可以提前知道哪些属性会在WMI Reference中查找哪些属性会导致操作系统出现问题。

编写应用程序的更强大的方法是检查您正在运行的操作系统,并且只尝试检索您知道可用的属性。您应该仍然将其全部包装在try / catch中,如其他答案中所示,以防万一其他意外发生,但是这个特定情况是您可以预期的并且应该编写代码而不是抛出和捕获异常。

答案 3 :(得分:1)

除了处理例外之外,如果您阅读FirmwareRevision关于此lblFirmware.Text = "Firmware: " + System.Environment.OSVersion.Version.Major <6 ? "Not Available in this OS" : moDisk["FirmwareRevision"].ToString(); 属性的类:

  

Windows Server 2003,Windows XP,Windows 2000和Windows NT 4.0:   此属性不可用。

您可以阻止此情况检查Windows版本

{{1}}