4

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.

Romasz
  • 29,662
  • 13
  • 79
  • 154
KAMAEL
  • 175
  • 2
  • 16

2 Answers2

4

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.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • But if I need to get info about system drive (free space / max space) etc. How I can get this? – KAMAEL Mar 23 '16 at 10:33
  • 1
    If you want to find the free space, you may try to use [this method](http://stackoverflow.com/a/23446870/2681948), though you have to pay attention to where you check the space - for example MusicLibrary can be both on phone/sd and so on. – Romasz Mar 23 '16 at 10:37
  • @Romasz is correct, but FYI you can list removable storage (i.e USB drives) - see my answer here http://stackoverflow.com/a/41009115/39094 – Adrian K Dec 07 '16 at 03:59
2

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.