Link to home
Start Free TrialLog in
Avatar of gwarguitar
gwarguitar

asked on

DriveInfo

I am trying to retrieve the drive list as Windows XP displays them. With the network path of network drives, and type of CD-ROM (i.e. DVD-RW, CD-RW, etc..)

is this possible? DriveInfo doesn't seem to return this information.
Avatar of athapa
athapa

use WMI to get that info.
try spomething like this

            //Get Drive list
            string[] drives = Directory.GetLogicalDrives();
            foreach (string drive in drives)
            {
               Console.WriteLine(drive);
            }
Avatar of gwarguitar

ASKER

I tried WMI, and looking in Server Explorer at my drives I don't see any property which contains this type of information. Any specific?
Avatar of Bob Learned
Win32_CDRomDrive:

// Add a reference to System.Management.dll

using System;
using System.Collections;
using System.Management;

namespace My.Computer.WMI
{

  public class CD_ROM
  {
 
    public enum DriveAvailability
    {
      Other = 1,
      Unknown,
      Running,
      Warning,
      NotApplicable,
      PowerOff,
      OffLine,
      OffDuty,
      Degraded,
      NotInstalled,
      InstallError,
      PowerSaveUnknown,
      PowerSaveLowPower,
      PowerSaveStandBy,
      PowerCycle,
      PowerSaveWarning
    }
    [Flags()]
      public enum DriveFlags
    {
      CaseSensitiveSearch = 1,
      CasePreservedNames = 2,
      UnicodeOnDisk = 4,
      PersistentAcls = 8,
      FileCompression = 16,
      VolumeQuotas = 32,
      SupportsSparseFiles = 64,
      SupportsReparsePoints = 128,
      SupportsRemoteStorage = 256,
      SupportsLongNames = 16384,
      VolumeCompressed = 32768,
      ReadOnlyVolume = 524289,
      SupportsObjectIds = 65536,
      SupportsEncryption = 131072,
      SupportsNamedStreams = 262144
    }
    public DriveAvailability Availability;
    public string Caption;
    public string CompressionMethod;
    public long DefaultBlockSize;
    public string Description;
    public string DeviceID;
    public string Drive;
    public bool DriveIntegrity;
    public DriveFlags FileSystemFlagsEx;
    public string Id;
    public string Manufacturer;
    public long MaxBlockSize;
    public int MaximumComponentLength;
    public long MaxMediaSize;
    public bool MediaLoaded;
    public string MediaType;
    public string MfrAssignedRevisionLevel;
    public long MinBlockSize;
    public string Name;
    public bool NeedsCleaning;
    public int NumberOfMediaSupported;
    public string PNPDeviceID;
    public bool PowerManagementSupported;
    public string RevisionLevel;
    public int SCSIBus;
    public int SCSILogicalUnit;
    public int SCSIPort;
    public int SCSITargetId;
    public long Size;
    public string Status;
    public string SystemName;
    public double TransferRate;
    public string VolumeName;
    public string VolumeSerialNumber;

    public static CD_ROM[] Drives
    {
      get
      {
       
        string query = "Select * from Win32_CDROMDrive";
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
        ArrayList listDrives = new ArrayList();
        foreach (ManagementObject currentObject in searcher.Get())
        {
          CD_ROM drive = new CD_ROM();
          drive.Availability = (DriveAvailability)Convert.ToInt32(currentObject["Availability"]);
          drive.Caption = (string)currentObject["Caption"];
          drive.CompressionMethod = (string)currentObject["CompressionMethod"];
          drive.DefaultBlockSize = Convert.ToInt64(currentObject["DefaultBlockSize"]);
          drive.Description = (string)currentObject["Description"];
          drive.DeviceID = (string)currentObject["DeviceID"];
          drive.Drive = (string)currentObject["Drive"];
          drive.DriveIntegrity = (bool)currentObject["DriveIntegrity"];
          drive.FileSystemFlagsEx = (DriveFlags)Convert.ToInt32(currentObject["FileSystemFlagsEx"]);
          drive.Id = (string)currentObject["Id"];
          drive.Manufacturer = (string)currentObject["Manufacturer"];
          drive.MaxBlockSize = Convert.ToInt64(currentObject["MaxBlockSize"]);
          drive.MaximumComponentLength = Convert.ToInt32(currentObject["MaximumComponentLength"]);
          drive.MaxMediaSize = Convert.ToInt64(currentObject["MaxMediaSize"]);
          drive.MediaLoaded = (bool)currentObject["MediaLoaded"];
          drive.MediaType = (string)currentObject["MediaType"];
          drive.MfrAssignedRevisionLevel = (string)currentObject["MfrAssignedRevisionLevel"];
          drive.MinBlockSize = Convert.ToInt64(currentObject["MinBlockSize"]);
          drive.Name = (string)currentObject["Name"];
          drive.NeedsCleaning = (bool)currentObject["NeedsCleaning"];
          drive.NumberOfMediaSupported = Convert.ToInt32(currentObject["NumberOfMediaSupported"]);
          drive.PNPDeviceID = (string)currentObject["PNPDeviceID"];
          drive.PowerManagementSupported = (bool)currentObject["PowerManagementSupported"];
          drive.RevisionLevel = (string)currentObject["RevisionLevel"];
          drive.SCSIBus = Convert.ToInt32(currentObject["SCSIBus"]);
          drive.SCSILogicalUnit = Convert.ToInt32(currentObject["SCSILogicalUnit"]);
          drive.SCSIPort = Convert.ToInt32(currentObject["SCSIPort"]);
          drive.SCSITargetId = Convert.ToInt32(currentObject["SCSITargetId"]);
          drive.Size = Convert.ToInt64(currentObject["Size"]);
          drive.Status = (string)currentObject["Status"];
          drive.SystemName = (string)currentObject["SystemName"];
          drive.TransferRate = Convert.ToDouble(currentObject["TransferRate"]);
          drive.VolumeName = (string)currentObject["VolumeName"];
          drive.VolumeSerialNumber = (string)currentObject["VolumeSerialNumber"];
          listDrives.Add(drive);
        } //foreach

        return (CD_ROM[])listDrives.ToArray(typeof(CD_ROM));

      } //get

    } //Drives

  } //class

} //namespace

Bob
i can't seem to get that code to return anything? am i missing something??? usage example please?
Exceptions?

How many results does 'searcher.Get())' return?

Bob
oh, wait. i'm stupid :P i typed something wrong.

i do get unhandled null exceptions on some of the properties such as NeedsCleaning and stuff

now to find the property i'm interested in! :)
What operating system are you working on?  For any properties that you don't care about, you can delete them, and remove the line that sets them.

Bob
on XP pro. nothing so far seems to be returning like "DVD-RW" or anything
What does 'Description' return?

Bob
CD-ROM
And, what does the Name return?

Bob
the model.

so i'm new to c#, and i really like the way you have this class, where you can just get properities in the GET area. I'm trying to duplicate this same type of thing with Operating System (SELECT * FROM Win32_OperatingSystem) but without the array, and not having much luck, could you give me a quick and dirty sample of that? mine seems to be stuck in an endless loop..

increased the points by the way...

    public class OperatingSystem
    {

        public string OSName;
        public string OSVersion;
        public string ComputerName;

        public static OperatingSystem OSInformation
        {
           
            get
            {
                string query = "SELECT * FROM Win32_OperatingSystem";
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                foreach (ManagementObject currentObject in searcher.Get())
                {
                    OSInformation.OSName = (string)currentObject["caption"];

                }
                return OSInformation;
            }
        }

    }
What version of .NET do you have?

Bob
2.0
With 2.0, the Server Explorer has a node element in the tree for WMI classes.  You can find the Management class Win32_OperatingSystem, and the properties that are associated with it.

Bob
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You were trying to use 'caption', and I am not sure if it's case sensitive.

Bob