473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Avalible System Drive Leters

How does on determain what the avalible system drive letters are (ex: C:,
D:, etc) in C#? Thanks!

-Noah Coad
Microsoft MVP & MCP
Nov 15 '05 #1
1 2718
You could use Environment.GetLogicalDrives. This returns you in an array of
strings the drives that are currently in use.

Another solution will be to use the Win32 API as follow:

[ DllImport( "Kernel32.dll", SetLastError=true )]
internal static extern int GetLogicalDrives();

private string GetFirstAvailableDrive()
{
// Get a mask representing drive currently in use bit0: is drive a:
int nDriveMask = GetLogicalDrives();

if (nDriveMask == 0)
{
// We had an error
throw new Exception("Unable to get list of drives that are currently
in use. Err is: " +
new Win32Exception().Message);
}

// get the first available drive (Starting from D: drive)
int nPos = 4; // Position on C: drive to start
for (int i = 3; i <= 25; i++)
{
nPos = nPos << 1; // Shift by one
if ((nDriveMask & nPos) == 0)
return (((char)(i + 65)).ToString());
}

// No device available
return null;
}
"Noah Coad [MCP & MVP]" <no**@coad.net> a écrit dans le message de
news:u4*************@TK2MSFTNGP10.phx.gbl...
How does on determain what the avalible system drive letters are (ex: C:,
D:, etc) in C#? Thanks!

-Noah Coad
Microsoft MVP & MCP

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Driv Dim Fso As New Scripting.FileSystemObjec For Each Drive As Scripting.Drive In Fso.Drive If...
4
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Drive Dim Fso As New Scripting.FileSystemObject For Each Drive As Scripting.Drive In Fso.Drives...
0
by: Ben Reese | last post by:
Using the FileSystemObject I can, at runtime find out which drive letter is asigned to the CD Rom Drive Dim Fso As New Scripting.FileSystemObject For Each Drive As Scripting.Drive In Fso.Drives...
0
by: Ben Reese | last post by:
Kind people on this discussion group have previously helped me to dynamically identify CD rom drives etc at runtime using objects in System.management +++++++++++++++++++++++++++++++++++++++ Dim...
1
by: Jon1996 | last post by:
How do you access the volume label for a hard drive through C#? I am very much a beginner with C# at the moment and am developing a windows explorer program to help me learn. I have tried the...
6
by: Rob R. Ainscough | last post by:
FSO appears to provide more information -- specifically about the type of drive (i.e. floppy vs. hard drive vs. network drive vs. removable drive i.e. CD-ROM/DVD) than the System.IO .NET framework...
3
by: Kondapanaidu | last post by:
Hi, I am using .NET 1.1 along with 2K Professional Operating System. How to create a new drive in a system which is not available in my system. Example: My System is having the A,B,C,D,E...
0
by: saraDotNet | last post by:
Hi, I'v Two Questions: 1-Is there a way to make an auto numbering avalible in vb.net form textbox as the one in Access db columns when we set the datatype as autonumbering?? 2- I'm working on a...
6
by: alex | last post by:
Hi, this always works: private ConsoleToTextbox myConsole; Console.SetOut(myConsole); But now (a few weeks later i started this project again) i get a non handeld security exception from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.