Why does Win32_DiskDrive SerialNumber return char 31 instead of an empty string?
<p>The following class returns the serial number of a drive.  When a regular fixed drive is passed, the serial number is returned correctly.  However, when a USB or SD drive is passed, it returns a string that is one character in length and that character has a decimal value of 31, at least on my computer it does.  The documentation says it should be blank.</p>

<p>Can a few people please test this to see if they get the same response and if anyone knows if that is the norm, I'd like to see where such is stated in the documentation.  This will need to be tested on Vista or Windows 7 or 8 as the SerialNumber property is not available on prior operating systems.</p>

<p>To get the serial number, call <code>SNTest.GetSerialNumber("C");</code>.  A message will appear showing the drive passed in, the serial number length and the decimal value of the first character if there is a first character.  The message I get on my computer for a USB drive is:<br>
Drive: H:<br>
Serial Number Length: 1<br>
First character's decimal value: 31  </p>

<pre><code>using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows;
using System.Management;
using System.Diagnostics;

    public static class SNTest
    {
        public static string GetSerialNumber(string drive)
        {
            return getSerialNumber(drive);
        }

        private static string getSerialNumber(string drive)
        {
            string sn = string.Empty;
            try
            {
                drive = drive.TrimEnd(':') + ":";
                string sqlLDTP = String.Format("associators of {{Win32_LogicalDisk.DeviceID='{0}'}} where AssocClass= Win32_LogicalDiskToPartition", drive);
                ManagementObjectCollection mocLDTP = new ManagementObjectSearcher(sqlLDTP).Get();
                foreach (ManagementObject moLDTP in mocLDTP)
                {
                    string sqlDDTDP = String.Format("associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass= Win32_DiskDriveToDiskPartition", moLDTP["DeviceID"]);
                    ManagementObjectCollection mocDDTDP = new ManagementObjectSearcher(sqlDDTDP).Get();
                    foreach (ManagementObject moDDTDP in mocDDTDP)
                    {
                        string sqlDD = String.Format("Select MediaType, PNPDeviceID, SerialNumber From Win32_DiskDrive Where DeviceID='{0}'", moDDTDP["DeviceID"]).Replace("\\", "\\\\");
                        ManagementObjectCollection mocDD = new ManagementObjectSearcher(sqlDD).Get();
                        foreach (ManagementObject moDD in mocDD)
                        {
                            sn = moDD["SerialNumber"].ToString();  // This property is not available for Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0.
                            int snLen = sn.Length;

                            byte[] b = Encoding.ASCII.GetBytes(sn);
                            int bLen = b.Count();

                            MessageBox.Show("Drive: " + drive + Environment.NewLine +
                                            "Serial Number Length: " + snLen + Environment.NewLine +
                                            (bLen &gt; 0 ? "First character's decimal value: " + b[0] : "No serial number."));
                        }
                    }
                }
            }
            catch { }
            return sn;
        }

    }
</code></pre>
