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?

Thanks,
Hendo

Recommended Answers

All 4 Replies

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

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?

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();
                        break;
                    }
                }
            }

THAT!!!! IS FREAKIN AWESOME!!
Thank you so much!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.