【问题标题】:How to get Drive Letter and Name (volume label)如何获取驱动器号和名称(卷标)
【发布时间】:2013-10-11 17:07:28
【问题描述】:

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

我如何得到这个?

【问题讨论】:

  • @Saravana,链接的问题没有可接受的答案,投票最高的问题可能不适合 OP

标签: c# drive driveinfo


【解决方案1】:

您正在寻找VolumeLabel 属性:

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

例子:

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

【讨论】:

  • +1。当它回答所问的问题时,也很可能不是 OP 正在寻找的(预期输出似乎正在寻找物理驱动器信息,部分由 DriveInfo.DriveType 覆盖)
  • 您可以使用GetVolumeInformationA() 从驱动器号中获取驱动器名称。例如,您可以查看此codetwee.blogspot.com/2020/05/…
【解决方案2】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 2020-10-30
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    相关资源
    最近更新 更多