代码之家  ›  专栏  ›  技术社区  ›  spacemonkeys

是否通过文件信息访问笔驱动器(MP3播放器)?

  •  1
  • spacemonkeys  · 技术社区  · 17 年前

    我想为用户提供一种设备选择,文件可以存储在这些设备上以进行物理删除,即不是硬盘驱动器。

    1 回复  |  直到 18 年前
        1
  •  9
  •   Mark Ingram    18 年前
    using System.IO;
    
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
      if (d.IsReady && d.DriveType == DriveType.Removable)
      {
        // This is the drive you want...
      }
    }
    

    DriveInfo类文档如下所示:

    http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

        2
  •  1
  •   sven    18 年前

    这是VB.NET代码,用于检查连接到计算机的任何可移动驱动器或CDRom驱动器:

    Me.lstDrives.Items.Clear()
    For Each item As DriveInfo In My.Computer.FileSystem.Drives
        If item.DriveType = DriveType.Removable Or item.DriveType = DriveType.CDRom Then
            Me.lstDrives.Items.Add(item.Name)
        End If
    Next
    

    将这段代码修改成c#等价的代码并不是那么难,等等 传动类型
    From MSDN:

    • 未知:
    • NOROOTDIRECORY:
    • 可拆卸: 驱动器是可移动存储设备,如软盘驱动器或USB闪存驱动器。
    • 该驱动器是一个固定磁盘。
    • 网络: 该驱动器是网络驱动器。
    • 驱动器是光盘设备,如CD或DVD-ROM。
    • 猛撞:
        3
  •  1
  •   Hath    18 年前

    在c#中,您可以通过使用System.IO.DriveInfo类获得相同的结果

    using System.IO;
    
    public static class GetDrives
    {
        public static IEnumerable<DriveInfo> GetCDDVDAndRemovableDevices()
        {
            return DriveInfo.GetDrives().
                Where(d => d.DriveType == DriveType.Removable
                && d.DriveType == DriveType.CDRom);
        }
    
    }
    
        4
  •  0
  •   Searush    14 年前

    这是VB.NET的完整模块:

    模块GetDriveNamesByType
    函数GetDriveNames(可选ByVal数据类型为DriveType=DriveType.Removable)为ListBox

    如果DN.DriveType=DType,则
    GetDriveNames.Items.Add(DN.Name)

    下一个
    端函数

    'Drive Types <br>
    'Unknown: The type of drive is unknown. <br>
    'NoRootDirectory: The drive does not have a root directory. <br>
    'Removable: The drive is a removable storage device, such as a floppy disk drive or a USB flash drive. <br>
    'Fixed: The drive is a fixed disk. <br>
    'Network: The drive is a network drive. <br>
    'CDRom: The drive is an optical disc device, such as a CD or DVD-ROM. <br>
    'Ram: The drive is a RAM disk. <br>