C# 如何获取可移动磁盘列表?

发布于 2024-07-27 02:43:40 字数 58 浏览 9 评论 0原文

我想在c#中获取可移动磁盘列表。 我想跳过本地驱动器。 因为我希望用户仅将文件保存在可移动磁盘中。

I want to get the list of removable disk in c#. I want to skip the local drives.
Because i want the user to save the file only in removable disk.

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

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

发布评论

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

评论(3

您需要为此方法引用System.IO

var driveList = DriveInfo.GetDrives();

foreach (DriveInfo drive in driveList)
{
    if (drive .DriveType == DriveType.Removable)
    {
    //Add to RemovableDrive list or whatever activity you want
    }    
}

或者对于 LINQ 粉丝:

var driveList = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable);

已添加

至于保存部分,据我所知,我认为您不能限制允许用户使用 SaveFileDialog 保存的位置,但您可以在显示 SaveFileDialog 后完成检查。

if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
  if (CheckFilePathIsOfRemovableDisk(saveFileDialog.FileName) == true)
  {
  //carry on with save
  }
  else
  {
  MessageBox.Show("Must save to Removable Disk, location was not valid");
  }
}

最好的选择是创建您自己的保存对话框,其中包含树视图,仅显示可移动驱动器及其内容供用户保存! 我会推荐这个选项。

希望这可以帮助

You will need to reference System.IO for this method.

var driveList = DriveInfo.GetDrives();

foreach (DriveInfo drive in driveList)
{
    if (drive .DriveType == DriveType.Removable)
    {
    //Add to RemovableDrive list or whatever activity you want
    }    
}

Or for the LINQ fans:

var driveList = DriveInfo.GetDrives().Where(d => d.DriveType == DriveType.Removable);

Added

As for the Saving part, as far as I know I don't think you can restrict where the user is allowed to save to using a SaveFileDialog, but you could complete a check after you have shown the SaveFileDialog.

if(saveFileDialog.ShowDialog() == DialogResult.OK)
{
  if (CheckFilePathIsOfRemovableDisk(saveFileDialog.FileName) == true)
  {
  //carry on with save
  }
  else
  {
  MessageBox.Show("Must save to Removable Disk, location was not valid");
  }
}

OR

The best option would be to create your own Save Dialog, which contains a tree view, only showing the removable drives and their contents for the user to save to! I would recommend this option.

Hope this helps

痴梦一场 2024-08-03 02:43:40

怎么样:

var removableDrives = from d in System.IO.DriveInfo.GetDrives()
                      where d.DriveType == DriveType.Removable;

How about:

var removableDrives = from d in System.IO.DriveInfo.GetDrives()
                      where d.DriveType == DriveType.Removable;
橘香 2024-08-03 02:43:40

您还可以使用 WMI 获取可移动驱动器列表。

ManagementObjectCollection drives = new ManagementObjectSearcher (
     "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();

根据评论进行编辑:

获取驱动器列表后,获取 GUID 并将它们添加到 SaveFileDialogInstance.CustomPlaces 集合中。

下面的代码需要一些调整......

System.Windows.Forms.SaveFileDialog dls = new System.Windows.Forms.SaveFileDialog();
dls.CustomPlaces.Clear();
dls.CustomPlaces.Add(AddGuidOfTheExternalDriveOneByOne);
....
....
dls.ShowDialog();

You can also use WMI to get the list of removable drives.

ManagementObjectCollection drives = new ManagementObjectSearcher (
     "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();

Edited based on comment:

After you get the list of drives get there GUID's and add them to SaveFileDialogInstance.CustomPlaces collection.

The code below need some tweaking...

System.Windows.Forms.SaveFileDialog dls = new System.Windows.Forms.SaveFileDialog();
dls.CustomPlaces.Clear();
dls.CustomPlaces.Add(AddGuidOfTheExternalDriveOneByOne);
....
....
dls.ShowDialog();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文