title: Getting an MSFT_Partition for a given MSFT_Volume
body: <p>I’m trying to get some details about a disk based on the drive letter and I’m having trouble getting the partition info for a given volume.</p>
  
  <p>This is what I’m trying:</p>
  
  <pre><code>var scope = new ManagementScope(@"\\localhost\ROOT\Microsoft\Windows\Storage");
  var query = new ObjectQuery("Associators of {MSFT_Volume.DriveLetter='" + upperCaseDiskLetter + "'} WHERE RESULTCLASS = MSFT_Partition");
  
  using (var partitionSearcher = new ManagementObjectSearcher(
      scope,
      query))
  {
      var partition = partitionSearcher.Get().Cast&lt;ManagementBaseObject&gt;().Single();
  </code></pre>
  
  <p>And getting this exception:</p>
  
  <pre><code>Exception thrown: 'System.Management.ManagementException' in System.Management.dll
  
  Additional information: Invalid object path
  </code></pre>
  
  <p>Maybe I’m approaching it wrong? I suppose a volume might exist on more than one partition? But still – I should get the “associators” query return some results unless MSFT_PartitionToVolume isn’t working for some reason?</p>
  
  <p>This is what I’m trying to collect:</p>
  
  <pre><code>using (var volumeSearcher = new ManagementObjectSearcher(
      @"\\localhost\ROOT\Microsoft\Windows\Storage",
      $"SELECT Size, SizeRemaining, DriveType FROM MSFT_Volume WHERE DriveLetter='{ upperCaseDiskLetter }'"))
  {
      var volume = volumeSearcher.Get().Cast&lt;ManagementBaseObject&gt;().Single();
  
      result = new DiskProperties
      {
          LastRetrieved = DateTime.Now,
          DriveType = GetDriveTypeDescription((UInt32)volume["DriveType"]),
          FreeSpace = (UInt64)volume["SizeRemaining"],
          PartitionSize = (UInt64)volume["Size"]
      };
  }
  
  var scope = new ManagementScope(@"\\localhost\ROOT\Microsoft\Windows\Storage");
  var query = new ObjectQuery("Associators of {MSFT_Volume.DriveLetter='" + upperCaseDiskLetter + "'} WHERE RESULTCLASS = MSFT_Partition");
  
  using (var partitionSearcher = new ManagementObjectSearcher(
      scope,
      query))
  {
      var partition = partitionSearcher.Get().Cast&lt;ManagementBaseObject&gt;().Single();
  
      using (var physicalDiskSearcher = new ManagementObjectSearcher(
                  @"\\localhost\ROOT\Microsoft\Windows\Storage",
                  $"SELECT Size, Model, MediaType FROM MSFT_PhysicalDisk WHERE DeviceID='{ partition["DiskNumber"] }'"))
      {
          var physicalDisk = physicalDiskSearcher.Get().Cast&lt;ManagementBaseObject&gt;().Single();
          result.DiskSize = (UInt64)physicalDisk["Size"];
          result.Model = physicalDisk["Model"].ToString();
          result.IsSsd =
              (UInt16)physicalDisk["MediaType"] == 4 ||
              SSDModelSubstrings.Any(substring =&gt; result.Model.ToLower().Contains(substring)); ;
  
          return result;
      }
  }
  </code></pre>