I'm working on an application that runs off of a thumb drive. The very first step in the application is to select the drive letter of the thumb drive. That populates a variable which is used throughout the program to note various paths on the thumb drive.

Is there a way to code this in C# to automatically see the drive letter that the thumb drive represents and therfore auto populates that Drv variable?


The driveinfo class should give you what you need. Probably, the easiest to use would be the volumelabel property. Here's the info on it

Edited 3 Years Ago by tinstaafl

Will that work in .NET 2.o?
EDIT yeah, they have the 2.0 section in that link. Now I have to figure out how to grab the drive letter and make it a useable variable. Is that right?

Edited 3 Years Ago by Hendo

yes, you can try this:

            string drv="";
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach(DriveInfo d in allDrives)
                if (d.IsReady)
                    if (d.VolumeLabel == "MyThumbDrive")
                        drv = d.RootDirectory.ToString();
This question has already been answered. Start a new discussion instead.