C#怎么可以获得在“我的电脑”那里各个盘的名字呢?

rt,如“本地磁盘(C:),新加卷(F:)”用DriveInfo只能获得盘名称(如“C:”)
2026年09月17日 09:22
有1个网友回答
网友(1):

using System;
using System.IO;

class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);

Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);

Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}
}

allDrives数组中含有电脑中所有硬盘的对象,你可以用0,1,2下标访问其中的一个盘,选择你需要的硬盘。每个盘对象中含有多个属性信息,其中 d.VolumeLabel就是你要的卷标信息,你试一下这个程序就知道了。