C# 如何获取驱动器号和名称(卷标)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19185655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:20:24  来源:igfitidea点击:

How to get Drive Letter and Name (volume label)

c#drivedriveinfo

提问by Jister13

I have a program that tells me all the hard disks/ usb's, but it only tells me the drive letter not the name. Here is what I have:

我有一个程序可以告诉我所有的硬盘/USB,但它只告诉我驱动器号而不是名称。这是我所拥有的:

DriveInfo[] drives = DriveInfo.GetDrives();
Console.WriteLine("Detected Drives: ");
for(int i = 0; i < drives.Count(); i++)
{
     Console.WriteLine("Drive " + i + ": " + drives[i].Name);
}

return drives;

and this prints:

这打印:

Drive 0: C:\
Drive 1: E:\

but I want the name such as

但我想要这样的名字

Drive 0: C:\ Local disk
Drive 1: E:\ Kingston USB

how do I get this?

我怎么得到这个?

采纳答案by Arran

You are looking for the VolumeLabelproperty:

您正在寻找的VolumeLabel房产:

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx

http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx

Example:

例子:

Console.WriteLine(drives[i].VolumeLabel);

回答by Napstar

Try this

尝试这个

 try
        {
            DriveInfo[] myDrives = DriveInfo.GetDrives();

            foreach (DriveInfo drive in myDrives)
            {
                Console.WriteLine("Drive:" + drive.Name);
                Console.WriteLine("Drive Type:" + drive.DriveType);

                if (drive.IsReady == true)
                {
                    Console.WriteLine("Vol Label:" + drive.VolumeLabel);
                    Console.WriteLine("File System: " + drive.DriveFormat);

                }
            }
        }
        catch (Exception)
        {

            throw;
        }