[英]How to get the list of removable disk in c#?
I want to get the list of removable disk in c#.我想在 c# 中获取可移动磁盘列表。 I want to skip the local drives.
我想跳过本地驱动器。 Because i want the user to save the file only in removable disk.
因为我希望用户只将文件保存在可移动磁盘中。
You will need to reference System.IO
for this method. 您需要为此方法引用
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
}
}
Or for the LINQ fans: 或者对于LINQ粉丝:
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. 至于Saving部分,据我所知,我不认为你可以限制用户可以保存到使用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");
}
}
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 希望这可以帮助
How about: 怎么样:
var removableDrives = from d in System.IO.DriveInfo.GetDrives()
where d.DriveType == DriveType.Removable;
This article looks to do the trick: 这篇文章看起来诀窍:
http://zayko.net/post/How-to-get-list-of-removable-drives-installed-on-a-computer-(C).aspx http://zayko.net/post/How-to-get-list-of-removable-drives-installed-on-a-computer-(C).aspx
You can also use WMI to get the list of removable drives. 您还可以使用WMI获取可移动驱动器列表。
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. 获得驱动器列表后,获取GUID并将它们添加到SaveFileDialogInstance.CustomPlaces集合中。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.