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

Jis*_*r13 6 c# drive driveinfo

我有一个程序告诉我所有的硬盘/ 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;
Run Code Online (Sandbox Code Playgroud)

这打印:

Drive 0: C:\
Drive 1: E:\
Run Code Online (Sandbox Code Playgroud)

但我想要这样的名字

Drive 0: C:\ Local disk
Drive 1: E:\ Kingston USB
Run Code Online (Sandbox Code Playgroud)

我怎么得到这个?

Arr*_*ran 12

您正在寻找VolumeLabel物业:

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

例:

Console.WriteLine(drives[i].VolumeLabel);
Run Code Online (Sandbox Code Playgroud)

  • +1.当它回答问题时,也很可能不是OP正在寻找的东西(预期输出似乎在寻找物理驱动器信息,部分由[DriveInfo.DriveType]覆盖(http://msdn.microsoft.com/ en-us/library/system.io.driveinfo.drivetype.aspx)) (2认同)

小智 5

尝试这个

 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;
        }
Run Code Online (Sandbox Code Playgroud)