所有物理驱动器的列表 [英] List of all physical drives

查看:18
本文介绍了所有物理驱动器的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取 UWP (Windows 10) 应用程序中所有物理驱动器的列表?我尝试使用 Windows.Storage.KnownFolders,但这样我只能从库中获取文件夹.

How to get list of all physical drives in UWP (Windows 10) App? I'm try to use Windows.Storage.KnownFolders, but this way I can get only folders from Library.

推荐答案

我知道你很久以前就问过这个问题,但我创建了一个问题 (在 UWP 中使用 Windows.Storage 命名空间获取内部驱动器) 提供我获取内部驱动器的方法并鼓励对更好的选择.

I know you asked this question a long time ago, but I created a question (Get Internal Drives Using Windows.Storage Namespace in UWP) to provide my method for getting internal drives and encourage feedback/discussion on a better alternative.

我有完全相同的问题需要解决,而我在网上能找到的所有其他内容都不适合我正在尝试做的事情.因此,通过将 BroadFileSystemAccess 属性添加到清单文件并在隐私设置"中为应用程序打开文件系统访问权限,可以调用 StorageFolder.GetFolderFromPathAsync 获取驱动器号,如果驱动器存在,它将返回 StorageFolder 的一个实例.

I had exactly the same problem to solve and everything else I can find online doesn't fit with what I'm trying to do. So, with the broadFileSystemAccess attribute added to the manifest file and File System access switched on for the app in Privacy Settings, it is possible to call StorageFolder.GetFolderFromPathAsync for a drive letter and it will return an instance of StorageFolder if the drive exists.

遗憾的是没有列出驱动器的方法,所以我写了一些东西来循环遍历字母表中的所有字母并调用 GetFolderFromPathAsync 来查看是否返回了驱动器句柄.

Sadly there isn't a method to list the drives, so I wrote something to cycle through all the letters of the alphabet and call GetFolderFromPathAsync to see if a drive handle is returned.

我创建的获取驱动器列表的方法如下:

The method I created to obtain the list of drives is as follows:

public List<StorageFolder> GetInternalDrives()
{
    string driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int driveLettersLen = driveLetters.Length;
    string removableDriveLetters = "";
    string driveLetter;

    List<StorageFolder> drives = new List<StorageFolder>();
    StorageFolder removableDevices = KnownFolders.RemovableDevices;
    IReadOnlyList<StorageFolder> folders = Task.Run<IReadOnlyList<StorageFolder>>(async () => await removableDevices.GetFoldersAsync()).Result;

    foreach (StorageFolder removableDevice in folders)
    {
        if (string.IsNullOrEmpty(removableDevice.Path)) continue;
        driveLetter = removableDevice.Path.Substring(0, 1).ToUpper();
        if (driveLetters.IndexOf(driveLetter) > -1) removableDriveLetters += driveLetter;
    }

    for (int curDrive = 0; curDrive < driveLettersLen; curDrive++)
    {
        driveLetter = driveLetters.Substring(curDrive, 1);
        if (removableDriveLetters.IndexOf(driveLetter) > -1) continue;

        try
        {
            StorageFolder drive = Task.Run<StorageFolder>(async () => await StorageFolder.GetFolderFromPathAsync(driveLetter + ":")).Result;
            drives.Add(drive);
        }
        catch (System.AggregateException) { }

    }

    return drives;
}

这是调用代码:

List<StorageFolder> drives = GetInternalDrives();
panScanParams.Children.Clear();
foreach (StorageFolder drive in drives)
{
    CheckBox cb = new CheckBox();
    cb.Content = drive.DisplayName;
    cb.IsChecked = true;
    panScanParams.Children.Add(cb);
}

虽然代码有效,但调用带有错误参数的方法并处理异常并不是一个好习惯.但由于缺乏合适的选择,我不知道还有什么其他选择.

Whilst the code works, it's not good practice to call methods with bad parameters and handle the exception. But with a lack of suitable alternative, I don't know what other choice there is.

这篇关于所有物理驱动器的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆