如何发现 USB 存储设备和可写 CD/DVD 驱动器 (C#)

发布于 2024-07-19 07:09:38 字数 112 浏览 1 评论 0原文

我如何发现在给定时间可用的任何 USB 存储设备和/或 CD/DVD 刻录机(使用 C# .Net2.0)。

我想向用户提供一种可以存储文件以进行物理删除的设备的选择 - 即不是硬盘驱动器。

How can I discover any USB storage devices and/or CD/DVD writers available at a given time (using C# .Net2.0).

I would like to present users with a choice of devices onto which a file can be stored for physically removal - i.e. not the hard drive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稳稳的幸福 2024-07-26 07:09:38
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

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...
  }
}

The DriveInfo class documentation is here:

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

酒浓于脸红 2024-07-26 07:09:38

这是 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# 等效的代码并不困难,并且可以使用更多driveType .
来自 MSDN:

  • 未知: 驱动器类型未知。
  • NoRootDirectory:驱动器没有根目录。
  • 可移动:驱动器是可移动存储设备,例如软盘驱动器或 USB 闪存驱动器。
  • 固定:驱动器是固定磁盘。
  • 网络:驱动器是网络驱动器。
  • CDRom: 驱动器是光盘设备,例如 CD 或 DVD-ROM。
  • Ram: 驱动器是 RAM 磁盘。

this is VB.NET code to check for any removable drives or CDRom drives attached to the computer:

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

it won't be that hard to modify this code into a c# equivalent, and more driveType's are available.
From MSDN:

  • 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.
月光色 2024-07-26 07:09:38

在 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);
    }

}

in c# you can get the same by using the System.IO.DriveInfo class

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);
    }

}
梦年海沫深 2024-07-26 07:09:38

这是 VB.NET 的完整模块:
导入 System.IO
模块 GetDriveNamesByType
函数 GetDriveNames(可选 ByVal DType As DriveType = DriveType.Removable) As ListBox
对于 My.Computer.FileSystem.Drives 中作为 System.IO.DriveInfo 的每个 DN
如果 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>

This is a complete module for VB.NET :
Imports System.IO
Module GetDriveNamesByType
Function GetDriveNames(Optional ByVal DType As DriveType = DriveType.Removable) As ListBox
For Each DN As System.IO.DriveInfo In My.Computer.FileSystem.Drives
If DN.DriveType = DType Then
GetDriveNames.Items.Add(DN.Name)
End If
Next
End Function
End Module

'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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文