DriveInfo Class
DriveInfo Class



The DriveInfo Class

The .NET 1.1 Framework provides several classes in the System.IO namespace for gathering information on files and directories. What is obviously lacking in this area was a similar class for gathering information on the drives, as well. This was remedied in the 2.0 version of the .NET Framework with the DriveInfo class. However, most developers have not yet transitioned to the new Visual Studio 2005 and 2.0 Framework, and many are not slated to perform this transition for quite some time.

While the .NET Framework often provides new functionality, in many cases, what is provided in 2.0 are items that could be built in the 1.0 or 1.1 .NET Frameworks. The DriveInfo class is just such an example.

Class Definition
The DriveInfo class for the 2.0 Framework is defined (simply) as follows:


public sealed class DriveInfo : ISerializable
{
    public DriveInfo(string driveName);

    public long AvailableFreeSpace { get; }
    public string DriveFormat { get; }
    public DriveType DriveType { get; }
    public bool IsReady { get; }
    public string Name { get; }
    public DirectoryInfo RootDirectory { get; }
    public long TotalFreeSpace { get; }
    public long TotalSize { get; }
    public string VolumeLabel { get; }

    public override bool Equals (object obj);
    public static DriveInfo[] GetDrives();
    public virtual int GetHashCode ();
    public Type GetType ();
    public static bool ReferenceEquals (object objectA, object objectB);
    public override string ToString ();



Win 32 API
In order to gather the information presented in this class, we need to access three (3) Win32 API calls: GetDriveType(), GetVolumeInformation(), and GetDiskFreeSpaceEx(). This are relatively common API calls, and the C# definitions are provided below:


    [DllImport("kernel32")]
    private static extern uint GetDriveType(string rootPathName);

    [DllImport("kernel32")]
    private static extern bool GetVolumeInformation(
        string rootPathName,
        StringBuilder volumeNameBuffer,
        uint VolumeNameSize,
        ref uint VolumeSerialNumber,
        ref uint MaximumComponentLength,
        ref uint FileSystemFlags,
        StringBuilder FileSystemNameBuffer,
        uint FileSystemNameSize);

    [DllImport("kernel32")]
    private static extern bool GetDiskFreeSpaceEx(
        string rootDirectoryName,
        ref long FreeBytesAvailable,
        ref long TotalNumberOfBytes,
        ref long TotalNumberOfFreeBytes);



DriveType Enumeration
The DriveType enumeration is also defined in the 2.0 Framework, in order to match the results of the GetDriveType API call. The values in the enumeration are defined to match the exact return code from the API call. The enumeration is defined as:

    public enum DriveType
    {
        Unknown = 0,
        NoRootDirectory = 1,
        Removeable = 2,
        Fixed = 3,
        Network = 4,
        CDRom = 5,
        Ram = 6
    }

Using these API and enumeration definitions, you can create your own version of the class, or download the DriveInfo class code sample here.

Download the ZIP File With the Class Source Code

Send Comments And FeedBack     Contact Us     Terms Of Use    Privacy Statement   
All items on this site are Copyright © 2006 by Sam Jones and Adaptive Intelligence. All rights reserved.