List of all physical drives

末鹿安然 提交于 2019-12-10 18:28:50

问题


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.


回答1:


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.

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.

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

And here is the calling code:

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.




回答2:


In UWP you cannot list all the files/drives just like that (with official API) - this is by design, probably for security reasons. Windows Store apps work are isolated and the access is only granted to limited resources/locations. In this case you are freely able to access virtual locations like MusicLibray, PicturesLibrary and so on. The list of access permisions you will find at MSDN.

If you want to access a file/folder from out of above scope, the user will have to grand the access to it for your app. For this purpose you can use pickers.



来源:https://stackoverflow.com/questions/36175414/list-of-all-physical-drives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!