Reputation: 4736
I have the below code which goes through and returns disk information. When running I notice that some WMI properties are not available on my computer (ie FirmwareRevision).
So, when I run the code VS is crashing out stating "not found". any idea how I put an exception catch in to state on the output of the object not found to state "Not Available".
I have been reading on exception catch but my amateurish way thus far is to explicitly define the exception I want to use...this program intends to run on loads of different servers so hoping it can intelligently figure out the WMI objects that any one server may not have.
The code is the below and currently it fails on (the last line):
"lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();"
Code:
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();
}
}
}
}
Upvotes: 1
Views: 330
Reputation: 136431
Additionally to handle the exceptions, if you read the documentation about this class for the FirmwareRevision
property :
Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0: This property is not available.
You can prevent this situation checking the Windows Version
lblFirmware.Text = "Firmware: " + System.Environment.OSVersion.Version.Major <6 ? "Not Available in this OS" : moDisk["FirmwareRevision"].ToString();
Upvotes: 1
Reputation: 3302
It's not a direct answer to your question, but the better answer is that you shouldn't rely on a try/catch. Catch statements are intended for situations which you cannot forsee, or which your program cannot gracefully handle. Neither of those apply here - you can actually know ahead of time which properties will cause problems on which operating systems by looking them up in the WMI Reference.
The more robust way to write your application is to check what operating system you're running on, and only attempt to retrieve the properties which you know are available. You should still wrap all of it in a try/catch as shown in other answers, just in case something else unexpected happens, but this specific case is something you can expect and should code around rather than throwing and catching exceptions.
Upvotes: 1
Reputation: 7759
Try and replace:
lblFirmware.Text = "Firmware: " +moDisk["FirmwareRevision"].ToString();
with:
lblFirmware.Text = "Firmware: " + moDisk["FirmwareRevision"] == null ? "Not Available" : moDisk["FirmwareRevision"].ToString();
Upvotes: 3
Reputation: 23811
Try this
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)
{
}
}
Upvotes: 2