2

I am making a query with WMI to check for a particular drive letter, if it does not exist then I want to run a method that will create it with specific settings. Now the problem I am facing is when I send a test query to see if the drive letter exists, it returns empty. No error or exception.

How would I handle this type of situation?

Thanks

ManagementObjectSearcher searcher =
                      new ManagementObjectSearcher("root\\cimv2",
                      @"SELECT * FROM Win32_Volume Where DriveLetter = '" + DriveLetter + "'");
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    drives.CurrentDriveLetter = queryObj["DriveLetter"] == null ? "null" : queryObj["DriveLetter"].ToString();
                    drives.CurrentDriveSize = queryObj["Capacity"] == null ? "null" : queryObj["Capacity"].ToString();
                    drives.CurrentDriveName = queryObj["Label"] == null ? "null" : queryObj["Label"].ToString();


                }
nGX
  • 1,038
  • 1
  • 20
  • 40
  • what is the `UpdateUI(drives) can you post that method as well..? also what is `drives and where is it declared.?` – MethodMan Apr 05 '13 at 21:33
  • drives is a class, just holds number/letter/label/size. UpdateUI is what updates the listview. Those 2 things can be ignored, what I am trying to figure out is a way to distinguish between a result WITH data and from results WITHOUT data. This is the tricky part for me since no exception is thrown – nGX Apr 05 '13 at 21:44
  • I tested the code and I get everything but the drive Letter also your `.ToString()` should be casted this way `(string)queryObj["DriveLetter"];` – MethodMan Apr 05 '13 at 21:48
  • you can look at this link and easily convert over to C# code as well http://msdn.microsoft.com/en-us/library/windows/desktop/aa394592(v=vs.85).aspx – MethodMan Apr 05 '13 at 21:53
  • but what if that drive doesn't exists? Try passing in a random drive letter and it will not error out so you have no idea if the query was valid or not – nGX Apr 05 '13 at 22:02
  • I figured your issue out ..you need to do one this twice keep the code you have and remove the `DriveName` and use the code that i am going to post to return the actual `DriveLetter` ok – MethodMan Apr 05 '13 at 22:04
  • you need to do some additional checks if there there is a difference between the object["DriveLetter"] for example existing and the value returning null.. so you need to do a check for null just because you use `queryObj["DriveLetter"] == null ? "null" : queryObj["DriveLetter"].ToString();` that only assigns the value `null` if null otherwise it returns the `drive letter` that line will never generate an error unless the object you are trying to reference does not exist.. – MethodMan Apr 05 '13 at 22:14
  • for example run the code `var mosNew = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");` and try to add `Console.WriteLine(mo["DriveLetter"]` it will Error because the Drive Letter should be the name when Selecting from `Wind32_LogicalDisk` – MethodMan Apr 05 '13 at 22:15

1 Answers1

2

Following your comments you only need determine if the collection returned by the ManagementObjectSearcher.Get method has elements. For that you can use the Count property.

Try this sample code

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {


        static void Main(string[] args)
        {
            try
            {
                ManagementScope Scope;                
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);
                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_Volume Where DriveLetter='X:' ");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
                if (Searcher.Get().Count==0)                
                {
                    Console.WriteLine("Do something, when the collection is empty.");                
                }
                else
                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0} {1}","Name",WmiObject["Name"]);// String

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
RRUZ
  • 134,889
  • 20
  • 356
  • 483