如何检测任何特定驱动器是不是是硬盘驱动器?
Posted
技术标签:
【中文标题】如何检测任何特定驱动器是不是是硬盘驱动器?【英文标题】:How to detect if any specific drive is a hard drive?如何检测任何特定驱动器是否是硬盘驱动器? 【发布时间】:2010-09-14 00:09:52 【问题描述】:在 C# 中,您如何检测特定驱动器是硬盘驱动器、网络驱动器、CDRom 还是软盘?
【问题讨论】:
【参考方案1】: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”。【参考方案2】:
DriveInfo.DriveType 应该适合你。
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
Console.WriteLine("Drive 0", d.Name);
Console.WriteLine(" File type: 0", d.DriveType);
【讨论】:
【参考方案3】:检查 System.IO.DriveInfo 类和 DriveType 属性。
【讨论】:
以上是关于如何检测任何特定驱动器是不是是硬盘驱动器?的主要内容,如果未能解决你的问题,请参考以下文章