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

本文关键字:卷标 驱动器 获取 何获取 | 更新日期: 2023-09-27 18:33:52

我有一个程序告诉我所有的硬盘/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;

这打印:

Drive 0: C:'
Drive 1: E:'

但我想要这样的名字

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

我怎么得到这个?

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

您正在寻找VolumeLabel属性:

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

例:

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

试试这个

 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;
        }