Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetDriveType (kernel32)
 
.
Summary

C# Signature:

  /// <summary>
  /// The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive
  /// </summary>
  /// <param name="lpRootPathName">A pointer to a null-terminated string that specifies the root directory and returns information about the disk.A trailing backslash is required. If this parameter is NULL, the function uses the root of the current directory.</param>
  [DllImport("kernel32.dll")]
  public static extern DriveType GetDriveType([MarshalAs(UnmanagedType.LPStr)] string lpRootPathName);

VB Signature:

  Private Declare Function GetDriveType Lib "kernel32" _
  Alias "GetDriveTypeA" (ByVal lpRootPathName As String) As Integer

User-Defined Types:

C# enum:

  public enum DriveType : uint
  {
    /// <summary>The drive type cannot be determined.</summary>
    Unknown = 0,    //DRIVE_UNKNOWN
    /// <summary>The root path is invalid, for example, no volume is mounted at the path.</summary>
    Error = 1,        //DRIVE_NO_ROOT_DIR
    /// <summary>The drive is a type that has removable media, for example, a floppy drive or removable hard disk.</summary>
    Removable = 2,    //DRIVE_REMOVABLE
    /// <summary>The drive is a type that cannot be removed, for example, a fixed hard drive.</summary>
    Fixed = 3,        //DRIVE_FIXED
    /// <summary>The drive is a remote (network) drive.</summary>
    Remote = 4,        //DRIVE_REMOTE
    /// <summary>The drive is a CD-ROM drive.</summary>
    CDROM = 5,        //DRIVE_CDROM
    /// <summary>The drive is a RAM disk.</summary>
    RAMDisk = 6        //DRIVE_RAMDISK
  }

VB enum:

Public Enum DriveType
    DRIVE_UNKNOWN = 0
    DRIVE_NO_ROOT_DIR = 1
    DRIVE_REMOVABLE = 2
    DRIVE_FIXED = 3
    DRIVE_REMOTE = 4
    DRIVE_CDROM = 5
    DRIVE_RAMDISK = 6
  End Enum

Notes:

  the above prototype fails with [MarshalAs(UnmanagedType.LPStr)]. However, this works:

None.

  public static extern DriveType GetDriveType([MarshalAs(UnmanagedType.LPTStr)] string lpRootPathName);

Tips & Tricks:

Please add some!

Sample Code:

Please add some!

using System.Runtime.InteropServices;
using System.IO;
namespace Volume
{
class Program
{
    [DllImport("kernel32.dll")]
    public static extern DriveType GetDriveType([MarshalAs(UnmanagedType.LPStr)] string lpRootPathName);

    static void Main(string[] args)
    {
        DriveType dt = GetDriveType("D:\\");//for me this is cdrom
    }
}
}


Alternative Managed API:

System.IO.DriveInfo.DriveType

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions