【发布时间】:2010-09-14 00:09:52
【问题描述】:
在 C# 中,您如何检测特定驱动器是硬盘驱动器、网络驱动器、CDRom 还是软盘?
【问题讨论】:
标签: c# hardware hard-drive
在 C# 中,您如何检测特定驱动器是硬盘驱动器、网络驱动器、CDRom 还是软盘?
【问题讨论】:
标签: c# hardware hard-drive
GetDrives() 方法返回一个 DriveInfo 类,该类具有对应于 System.IO.DriveType 枚举的 DriveType 属性:
public enum DriveType
{
Unknown, // The type of drive is unknown.
NoRootDirectory, // The drive does not have a root directory.
Removable, // The drive is a removable storage device,
// such as a floppy disk drive or a USB flash drive.
Fixed, // The drive is a fixed disk.
Network, // The drive is a network drive.
CDRom, // The drive is an optical disc device, such as a CD
// or DVD-ROM.
Ram // The drive is a RAM disk.
}
这是一个来自 MSDN 的稍微调整的示例,它显示所有驱动器的信息:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}, Type {1}", d.Name, d.DriveType);
}
【讨论】:
DriveType 为外部 USB 硬盘返回“DriveType.Fixed”。
DriveInfo.DriveType 应该适合你。
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
}
【讨论】:
检查 System.IO.DriveInfo 类和 DriveType 属性。
【讨论】: