【问题标题】:How to detect if any specific drive is a hard drive?如何检测任何特定驱动器是否是硬盘驱动器?
【发布时间】:2010-09-14 00:09:52
【问题描述】:

在 C# 中,您如何检测特定驱动器是硬盘驱动器、网络驱动器、CDRom 还是软盘?

【问题讨论】:

    标签: c# hardware hard-drive


    【解决方案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 属性。

      【讨论】:

        猜你喜欢
        • 2010-09-14
        • 2010-10-09
        • 2014-09-01
        • 2016-07-15
        • 2012-07-10
        • 2011-07-18
        • 2012-11-03
        • 2015-12-24
        相关资源
        最近更新 更多