How to get the harddrive drive letter? (or how many drive in a computer)

  • Thread starter Thread starter Jason Chan
  • Start date Start date
you may use the Windows API method below:DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lpBuffer
);
 
You can use WMI

Here is an example:

/* CODE BEGIN */
using System;
using System.Management;

public class network
{
public static void Main()
{
SelectQuery query = new SelectQuery( "select name, FreeSpace from
win32_logicaldisk where drivetype=3" );
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);

foreach( ManagementObject mo in searcher.Get() )
{
Console.WriteLine( "Drive letter is: {0}", mo["name"] );
Console.WriteLine( "Drive's free space is: {0}", mo["FreeSpace"] );
}

// Here to stop app from closing
Console.WriteLine( "\n\nPress Return to exit." );
Console.Read();
}
}
/* CODE END */


I have an example app here for network drives.
http://www.publicjoe.f9.co.uk/csharp/snip/snip010.html
The page has links to relevant MSDN pages as well.

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html
 
Back
Top