我有一个程序告诉我所有的硬盘/ 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
我该怎么做?
答案 0 :(得分:12)
您正在寻找VolumeLabel
属性:
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx
示例:
Console.WriteLine(drives[i].VolumeLabel);
答案 1 :(得分:4)
试试这个
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;
}