开发者

How do you get a USB Drive's Name in C#?

开发者 https://www.devze.com 2023-01-15 11:39 出处:网络
I need to get the name of my 开发者_运维问答USB Drive. Say I rename my USB Drive to \"ZeroErrors\".

I need to get the name of my 开发者_运维问答USB Drive.

Say I rename my USB Drive to "ZeroErrors".

and it is Drive letter "G:\" I want to use the drive letter to get the name of the USB Drive.


Look at the DriveInfo class.

string name = new DriveInfo("g").VolumeLabel;

or for all removable drives:

DriveInfo.GetDrives()
         .Where(x => x.DriveType == DriveType.Removable)
         .Select(x => x.VolumeLabel)


The following code should do the trick:

        DriveInfo drive = new DriveInfo("G:");
        Console.WriteLine(drive.VolumeLabel);


This link on MSDN and this on CodeProject should help you to get informations from your drives (included the USB).

But instead of use the letter of the drive, use the reference number.

0

精彩评论

暂无评论...
验证码 换一张
取 消