Desktop Functions:

   Smart Device Functions:

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

Terms of Use
Privacy Policy
getlogicaldrives (kernel32)
Fills a buffer with strings that specify valid drives in the system. If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character.

C# Signature:

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetLogicalDriveStrings(uint bufferLength, [Out] char[] buffer);

User-Defined Types:



bufferLength, The maximum size of the buffer pointed to by buffer, in TCHARs. This size does not include the terminating null character. If this parameter is zero, buffer is not used.

buffer, A pointer to a buffer that receives a series of null-terminated strings, one for each valid drive in the system, plus with an additional null character. Each string is a device name.


DO NOT define using a StringBuilder, because only the first string would be returned.

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint GetLogicalDriveStrings(uint bufferLength, [Out] StringBuilder buffer);

Tips & Tricks:

Since arrays are reference types, we can pass an array of chars. Initialize your array prior to calling the function:

char[] buffer = new char[bufferLength];

Sample Code:

using System;
using System.Collections.Specialized;
using System.Runtime.InteropServices;

public class Mainline
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern uint GetLogicalDriveStrings(uint bufferLength, [Out] char[] buffer);

    public static void Main()
       const int size = 512;
       char[] buffer = new char[size];
       uint code = GetLogicalDriveStrings(size, buffer);

       if (code == 0)
      Console.WriteLine("Call failed");

       StringCollection list = new StringCollection();
       int start = 0;
       for (int i = 0;  i < code;  ++i)
      if (buffer[i] == 0)
         string s = new string(buffer, start, i - start);
         start = i + 1;

       foreach (string s in list)

Alternative Managed API:

public static string[] System.Environment.GetLogicalDrives();

public static System.IO.DriveInfo[] System.IO.DriveInfo.GetDrives();


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 directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version