簡體   English   中英

如何獲取驅動器號和名稱(卷標)

[英]How to get Drive Letter and Name (volume label)

我有一個程序告訴我所有的硬盤/ 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;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM