title: c# error capturing wmi
body: <p>I have the below code that goes through physical disks and displays WMI settings. </p>
  
  <p>I have noticed that if a WMI parameter does not exists it does not display anything (which is fine) but this interferes with subsequent WMI parameters that DO exists on the server and they now do not show anything...even they they should..example below.</p>
  
  <p>So, what I mean is that the code goes through this part:</p>
  
  <pre><code> 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();
  </code></pre>
  
  <p>It first gets SystemName,
  Then it gets Type and so on.</p>
  
  <p>Nowthen, if on a computer the WMI parameter of say "Availability" does not exist then it will not display that. That is fine. HOWEVER, immediatly after "Availability" there is "Bytes per sector" parameter and others. The parameters of these do not display anything as well (even though I know they should show something as they do exist on the server - tested via powershell). </p>
  
  <p>It is as though the lack of information from "Availability" is interfering with the parameters after and not displaying anything.</p>
  
  <p>The only way I can think at the moment of fixing this (so that even if availability shows nothing but subsequent parameters show the data they need) is to possibly add a IF THEN within each WMI paramter - not nice coding but can't think of a way of fixing this.</p>
  
  <p>So an exmaple would be IF [Availbility] then message "Avialbility = whatever" ELSE "WMI parameter not found". This way each WMI parameter has its own error checking. Not nice coding I imagine.. </p>
  
  <p>There is already an error capture in case a WMI parameter does not exist then a comment is displayed to the end user.</p>
  
  <p>Here's the code: </p>
  
  <pre><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)
          {
              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());
              }
          }
      }
  }
  </code></pre>