locked
Win32_LogicalDiskToPartition does not return any results RRS feed

  • Question

  • I am pretty new at coding C# and WMI, so if this comes up as a really simple solution please forgive me. I've spent the past day or so trying to find a solution to this and it's either not there or I've not found the right keywords.

    I am using WMI to retrieve the drive letter for a PCMCIA card on a laptop. The purpose of this is to scan the PC Card for a specific set of files and copy them to a known folder. I have two WinXP SP2 machines and a Win7 machine that return different results.

    The code below returns the drive correct drive on the Win7 drive and one of the WinXP machines, but fails on the other WinXP machine. I've narrowed it down to the Win32_LogicalDiskToPartition query, where it does not return any results. I've extended this with a test using WMIExplorer and found that SELECT * from Win32_LogicalDiskToPartition does not return any results on the machine that fails.

    The only difference that I am able to discern is that the user accounts on the two machines that work have local admin privileges whereas the machine that fails does not. Is this a limitation of the Win32_LogicalDiskToPartition class, or should I be looking somewhere else?

    ----------------------CUT--------------------------------

      public static void PCCardDetect(string strDrive){
       
       // define variables.
       string strQuery, strSystemDrive, strTemp;
       ConnectionOptions conOptions;
       ManagementScope scope;
       ManagementObjectSearcher colLogicalDisk, colPartitions, colDisks, colIDEInterfaces;
       
       // Connect to WMI service
       try {
        conOptions = new ConnectionOptions();
        conOptions.Impersonation = ImpersonationLevel.Impersonate;
        scope = new ManagementScope("\\root\\cimv2", conOptions);
        scope.Connect();
        LogFile.Log("Connected to WMI");
         
       } catch (Exception ex) {
        LogFile.Log("Could not connect to WMI " + ex.Message);
        return;
       }  
       
       //Trace logical drive (system drive) down to disk controller and output details
       strQuery = "SELECT * FROM Win32_LogicalDisk WHERE Name = " + "'" + strDrive + "'";
       LogFile.Log("Logical Disk Query: " + strQuery);
       colLogicalDisk = new ManagementObjectSearcher(strQuery);
       try {
        foreach(ManagementObject objLogicalDisk in colLogicalDisk.Get()){
         strSystemDrive = Convert.ToString(objLogicalDisk["DeviceID"]);
         strQuery = "ASSOCIATORS OF {Win32_LogicalDisk.DeviceID='" + strSystemDrive + "'} WHERE AssocClass = Win32_LogicalDiskToPartition";
         LogFile.Log("Partitions Query: " + strQuery);
         colPartitions = new ManagementObjectSearcher(strQuery);
         try {
          foreach(ManagementObject objPartition in colPartitions.Get()){
           strQuery = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + objPartition["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition";
           LogFile.Log("Disks Query: " + strQuery);
           colDisks = new ManagementObjectSearcher(strQuery);
           try {
            foreach(ManagementObject objDisk in colDisks.Get()){
             strTemp = Convert.ToString(objDisk["PNPDeviceID"]);
             strQuery = "ASSOCIATORS OF {Win32_PNPEntity.DeviceID='" + strTemp + "'} WHERE AssocClass = Win32_IDEControllerDevice";
             LogFile.Log("IDE Interfaces Query: " + strQuery);
             colIDEInterfaces = new ManagementObjectSearcher(strQuery);
             try {
              foreach(ManagementObject objIDEInterface in colIDEInterfaces.Get()){
               strTemp = Convert.ToString(objIDEInterface["PNPDeviceID"]);
               LogFile.Log("Drive:\t" + strSystemDrive);
               LogFile.Log("PNP ID:\t" + objIDEInterface["PNPDeviceID"]);
               if (strTemp.Substring(0,6)=="PCMCIA") {
                LogFile.Log("PC Card Drive Found at: " + strSystemDrive);
                //strPCCardDrive = strSystemDrive;
                return;
               }
              } 
             } catch (Exception ex) {
              LogFile.Log("WMI Query Failed: " + strQuery + " Exception: " + ex.Message);
             }
            } 
           } catch (Exception ex) {
            LogFile.Log("WMI Query Failed: " + strQuery + " Exception: " + ex.Message);
           }
          } 
         } catch (Exception ex) {
          LogFile.Log("WMI Query Failed: " + strQuery + " Exception: " + ex.Message);
         }
        } 
       } catch (Exception ex) {
        LogFile.Log("WMI Query Failed: " + strQuery + " Exception: " + ex.Message);
        LogFile.Log("Exception Source: " + ex.Source);
        LogFile.Log("Exception Target: " + ex.TargetSite);
        LogFile.Log("Exception Stack: " + ex.StackTrace.ToString());
       }
       return;
      }

    ----------------------CUT--------------------------------

    Cheers

    Jason

    Friday, July 23, 2010 6:33 AM

Answers

All replies

  • Hi Jason,

    Why not use the DriveInfo class? It has a constructor that takes the drive name. WMI is not the best choice, in my opinion, given your scenario. I highly recommend you review the classes available in the System.IO namespace. They will expose just about everything you need to do with files. Good luck!

    -Scosby

    • Marked as answer by Nancy Shao Friday, July 30, 2010 9:06 AM
    Friday, July 23, 2010 9:37 PM
  • Hi Scosby,

    The application this will be used for will copy log files that are stored on a PCMCIA card to the server. I have just thought that I should change the function above to a boolean to return true if the drive letter passed is a PCMCIA drive or not, which I'll update shortly.

    While the DriveInfo class returns information on size and type, it does not go down to the driver level. Unfortunately I can not guarentee the manufacturer of the card, the size of the card or the drive that the card is mapped too. The format will be FAT32 and they show up as removable drives - but so do USB thumbdrives.

    However, thanks for the tip on the System.IO namespace... that will save me some hunting and research for the rest of the app.

    Cheers

    Jason

    Friday, July 23, 2010 11:27 PM