USB Drive Removal Code in C#
<p>When I use this function in c#, it is able to get drive letter but, when I remove the USB stick and test this function, it doesnt go to the Exception.</p>

<p>So could someone help me with where I am going wrong in the function code?</p>

<pre><code>       public void GetDriveLetter()
    {

        try
        {

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskDrive where InterfaceType='USB'");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                foreach (ManagementObject b in queryObj.GetRelated("Win32_DiskPartition"))
                {
                    foreach (ManagementBaseObject c in b.GetRelated("Win32_LogicalDisk"))
                    {
                        // writer.WriteLine("{0}" + "\\", c["Name"].ToString()); // here it will print drive letter
                        usbDriveLetter = String.Format("{0}" + "\\", c["Name"].ToString());
                    }
                }

            }

        }
        catch (ManagementException e)
        {
            MessageBox.Show(e.StackTrace);
        }
        //CombinedPath = System.IO.Path.Combine(usbDriveLetter.Trim(), path2.Trim());           
    }
</code></pre>
