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.
is this possible? DriveInfo doesn't seem to return this information.
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);
}
//Get Drive list
string[] drives = Directory.GetLogicalDrives
foreach (string drive in drives)
{
Console.WriteLine(drive);
}
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?
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(q uery);
ArrayList listDrives = new ArrayList();
foreach (ManagementObject currentObject in searcher.Get())
{
CD_ROM drive = new CD_ROM();
drive.Availability = (DriveAvailability)Convert .ToInt32(c urrentObje ct["Availa bility"]);
drive.Caption = (string)currentObject["Cap tion"];
drive.CompressionMethod = (string)currentObject["Com pressionMe thod"];
drive.DefaultBlockSize = Convert.ToInt64(currentObj ect["Defau ltBlockSiz e"]);
drive.Description = (string)currentObject["Des cription"] ;
drive.DeviceID = (string)currentObject["Dev iceID"];
drive.Drive = (string)currentObject["Dri ve"];
drive.DriveIntegrity = (bool)currentObject["Drive Integrity" ];
drive.FileSystemFlagsEx = (DriveFlags)Convert.ToInt3 2(currentO bject["Fil eSystemFla gsEx"]);
drive.Id = (string)currentObject["Id" ];
drive.Manufacturer = (string)currentObject["Man ufacturer" ];
drive.MaxBlockSize = Convert.ToInt64(currentObj ect["MaxBl ockSize"]) ;
drive.MaximumComponentLeng th = Convert.ToInt32(currentObj ect["Maxim umComponen tLength"]) ;
drive.MaxMediaSize = Convert.ToInt64(currentObj ect["MaxMe diaSize"]) ;
drive.MediaLoaded = (bool)currentObject["Media Loaded"];
drive.MediaType = (string)currentObject["Med iaType"];
drive.MfrAssignedRevisionL evel = (string)currentObject["Mfr AssignedRe visionLeve l"];
drive.MinBlockSize = Convert.ToInt64(currentObj ect["MinBl ockSize"]) ;
drive.Name = (string)currentObject["Nam e"];
drive.NeedsCleaning = (bool)currentObject["Needs Cleaning"] ;
drive.NumberOfMediaSupport ed = Convert.ToInt32(currentObj ect["Numbe rOfMediaSu pported"]) ;
drive.PNPDeviceID = (string)currentObject["PNP DeviceID"] ;
drive.PowerManagementSuppo rted = (bool)currentObject["Power Management Supported" ];
drive.RevisionLevel = (string)currentObject["Rev isionLevel "];
drive.SCSIBus = Convert.ToInt32(currentObj ect["SCSIB us"]);
drive.SCSILogicalUnit = Convert.ToInt32(currentObj ect["SCSIL ogicalUnit "]);
drive.SCSIPort = Convert.ToInt32(currentObj ect["SCSIP ort"]);
drive.SCSITargetId = Convert.ToInt32(currentObj ect["SCSIT argetId"]) ;
drive.Size = Convert.ToInt64(currentObj ect["Size" ]);
drive.Status = (string)currentObject["Sta tus"];
drive.SystemName = (string)currentObject["Sys temName"];
drive.TransferRate = Convert.ToDouble(currentOb ject["Tran sferRate"] );
drive.VolumeName = (string)currentObject["Vol umeName"];
drive.VolumeSerialNumber = (string)currentObject["Vol umeSerialN umber"];
listDrives.Add(drive);
} //foreach
return (CD_ROM[])listDrives.ToArr ay(typeof( CD_ROM));
} //get
} //Drives
} //class
} //namespace
Bob
// 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(q
ArrayList listDrives = new ArrayList();
foreach (ManagementObject currentObject in searcher.Get())
{
CD_ROM drive = new CD_ROM();
drive.Availability = (DriveAvailability)Convert
drive.Caption = (string)currentObject["Cap
drive.CompressionMethod = (string)currentObject["Com
drive.DefaultBlockSize = Convert.ToInt64(currentObj
drive.Description = (string)currentObject["Des
drive.DeviceID = (string)currentObject["Dev
drive.Drive = (string)currentObject["Dri
drive.DriveIntegrity = (bool)currentObject["Drive
drive.FileSystemFlagsEx = (DriveFlags)Convert.ToInt3
drive.Id = (string)currentObject["Id"
drive.Manufacturer = (string)currentObject["Man
drive.MaxBlockSize = Convert.ToInt64(currentObj
drive.MaximumComponentLeng
drive.MaxMediaSize = Convert.ToInt64(currentObj
drive.MediaLoaded = (bool)currentObject["Media
drive.MediaType = (string)currentObject["Med
drive.MfrAssignedRevisionL
drive.MinBlockSize = Convert.ToInt64(currentObj
drive.Name = (string)currentObject["Nam
drive.NeedsCleaning = (bool)currentObject["Needs
drive.NumberOfMediaSupport
drive.PNPDeviceID = (string)currentObject["PNP
drive.PowerManagementSuppo
drive.RevisionLevel = (string)currentObject["Rev
drive.SCSIBus = Convert.ToInt32(currentObj
drive.SCSILogicalUnit = Convert.ToInt32(currentObj
drive.SCSIPort = Convert.ToInt32(currentObj
drive.SCSITargetId = Convert.ToInt32(currentObj
drive.Size = Convert.ToInt64(currentObj
drive.Status = (string)currentObject["Sta
drive.SystemName = (string)currentObject["Sys
drive.TransferRate = Convert.ToDouble(currentOb
drive.VolumeName = (string)currentObject["Vol
drive.VolumeSerialNumber = (string)currentObject["Vol
listDrives.Add(drive);
} //foreach
return (CD_ROM[])listDrives.ToArr
} //get
} //Drives
} //class
} //namespace
Bob
ASKER
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
How many results does 'searcher.Get())' return?
Bob
ASKER
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! :)
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
Bob
ASKER
on XP pro. nothing so far seems to be returning like "DVD-RW" or anything
What does 'Description' return?
Bob
Bob
ASKER
CD-ROM
And, what does the Name return?
Bob
Bob
ASKER
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(q uery);
foreach (ManagementObject currentObject in searcher.Get())
{
OSInformation.OSName = (string)currentObject["cap tion"];
}
return OSInformation;
}
}
}
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(q
foreach (ManagementObject currentObject in searcher.Get())
{
OSInformation.OSName = (string)currentObject["cap
}
return OSInformation;
}
}
}
What version of .NET do you have?
Bob
Bob
ASKER
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
Bob
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You were trying to use 'caption', and I am not sure if it's case sensitive.
Bob
Bob