UWP -- Accessing "My Documents" folder DENIED

XamtasticXamtastic Member ✭✭✭

I can access the APP directory to save/create files. However anything outside the APP dir is inaccessible.

For UWP I keep getting:
Access to the path 'C:\Users[user]\ Documents ' is denied.
Access to the path 'C:\Users[user]\ Pictures ' is denied.

I am using:
directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

Manifest:

    <uap:Capability Name="picturesLibrary" />
    <uap:Capability Name="documentsLibrary" />
    <uap:Capability Name="musicLibrary" />
    <uap:Capability Name="videosLibrary" />
    <uap:Capability Name="removableStorage" />

Microsoft documentation makes it seem like this will never be accessible, but I really have to be reading that wrong.

Any assistance is appreciated!!!

Tagged:

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    The way you access the folder in uwp is not correct.
    You need to use the Dependency Service to achieve this. The implementation varies between Android, iOS and Windows platforms, where the methods for the different platforms do similar things but in different ways.
    For example:
    On Android:

        string fullPath = Path.Combine(
            System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), 
            fileName);
        return fullPath;
    

    On iOS:

        string filename = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
            "..", "Library", fileName);
        return filename;
    

    On WinRT:

        var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
        string fullPath = Path.Combine(path, fileName);
        return fullPath;
    

    On UWP:

        var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
        string fullPath = Path.Combine(path, fileName);
        return fullPath;
    
  • XamtasticXamtastic Member ✭✭✭

    Actually I had gotten the correct DIR location. The problem is that I can't access the folder.

    To confirm it was correct I put it into a variable and went into debug, copied the DIR location (replaced the double "\", [Win key]+"R" and paste....DIR location is good. Re-running the app, I manually paste the DIR location in and same access problem.

    So DIR location is good.
    Access/Permissions is the problem.

  • XamtasticXamtastic Member ✭✭✭
    Accepted Answer

    Ended up looping back on this:
    https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows

    Just makes life easier. Not a custom selector (which is what I was going for) but it works! No permissions issues!!!

Sign In or Register to comment.