1

I am using the following code

            StorageFolder folder;
            if (initial) folder = await StorageFolder.GetFolderFromPathAsync(@"C:\");
            else { use folder picker }

and every time i try to get a Storage Drive it returns an error, what i have noticed is that if i use the Folder Picker then it doesnt throw an exception.

I am not sure what is causing this and it seems pretty irritating that my users have to specify the drive instead of my application automatically getting it.

Exception Description "Access is denied.\r\n"

Connor S
  • 264
  • 1
  • 3
  • 12
  • Maybe the error is generated due to the sandboxing system. – ganchito55 Mar 26 '16 at 13:26
  • Well what does the exception say? – Jon Skeet Mar 26 '16 at 13:31
  • @JonSkeet 'Access is denied.\r\n' – Connor S Mar 26 '16 at 13:33
  • 1
    Right - please add that information into the question. *Always* include the details of an error when you see that there is an error. – Jon Skeet Mar 26 '16 at 13:33
  • @JonSkeet didnt think that would be helpful, it certainly didnt help me, thats why i came here – Connor S Mar 26 '16 at 13:34
  • Always assume the error information will be helpful. I'd argue that it still *is* helpful, in that it shows it's a permission issue rather than anything else. When you say that you can use the folder picker without an error, does that include picking `c:\`? Can you then use that within your app without errors? – Jon Skeet Mar 26 '16 at 13:35

2 Answers2

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 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.

When using the picker you won't get exception, hence the user has granted access to your app.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • I already know this but it seems strange that they would allow me to use something like the path C:\Windows\ and not C:\ – Connor S Mar 26 '16 at 13:28
  • @ConnorS Are you able to access storagefile/folder by path c:\Windows? If you are accesing it via picker, then this is normal - accessing by picker means that user has granted access to the app. – Romasz Mar 26 '16 at 13:32
  • i can access it both ways, even without the users permission – Connor S Mar 26 '16 at 13:34
  • @ConnorS Weird. Nevertheless I wouldn't depend on this access via path, at least in UWP apps. Maybe it comes from some kind of library association, I'm not sure. I'm even curious if it would be able to access file in C:\Windows directly by path. – Romasz Mar 26 '16 at 13:37
  • I can confirm it does, i have a system that iterates the entire filesystem but one the files and folders it needs to, to cut resources usage to a minimum – Connor S Mar 26 '16 at 13:39
  • 1
    @ConnorS I've also encountered some file access problems some time ago, which I couldn't understand. Maybe [this Peter's Torr comment](http://stackoverflow.com/questions/27875608/different-app-behaviour-when-deployed-locally-and-from-store#comment44162948_27880002) can be also addressed here. – Romasz Mar 26 '16 at 13:49
0

If you want to use the user folder,and you should make he pick the folder.

Windows.Storage.Pickers.FolderPicker folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
folderPicker.FileTypeFilter.Add(".txt");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)           
{                            
     //do            
}
lindexi
  • 4,182
  • 3
  • 19
  • 65
  • I think you may want to know how to get the read access in the `C:\ ` folder.And if you make the `GetFolderFromPathAsync(@"C:\");` you should have the read access.And you can get the read access without user pick only LocalFolder and the Library you ask the user in install. – lindexi Mar 28 '16 at 01:01
  • We have already concluded that you cant due to the restrictive access Windows Universal gives us for security measures – Connor S Mar 28 '16 at 12:12