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
How to get Drive Letter and Name (volume label)
提问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 VolumeLabel
property:
您正在寻找的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;
}