如何获得可移动磁盘在C#中的列表?磁盘、如何获得、列表

2023-09-02 21:35:47 作者:看破红尘

我想获得可移动磁盘的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.

推荐答案

您需要引用 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
    }    
}

或者为LIN​​Q球迷:

Or for the LINQ fans:

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

添加 至于储蓄的部分,据我所知,我不认为你可以限制,其中允许用户保存到使用SaveFileDialog,但你可以填写一张支票,你已经证明了SaveFileDialog后。

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

最好的办法是创建自己的保存对话框,其中包含一个树形视图,只显示可移动驱动器及其内容为用户节省!我会建议使用此选项。

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.

希望这有助于