C#实现获取硬盘卷标号
以下是使用C#实现获取硬盘卷标号的完整源码:
using System;
using System.IO;
public class DiskVolumeLabel
{
public static void Main()
{
// 获取所有逻辑驱动器名称
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
// 获取驱动器信息
DriveInfo driveInfo = new DriveInfo(drive);
// 检查驱动器类型是否为固定磁盘
if (driveInfo.DriveType == DriveType.Fixed)
{
Console.WriteLine("驱动器: " + driveInfo.Name);
Console.WriteLine("卷标号: " + GetVolumeLabel(driveInfo.Name));
Console.WriteLine();
}
}
}
// 获取驱动器的卷标号
public static string GetVolumeLabel(string driveName)
{
string volumeLabel = "";
try