How to create a folder in a UWP application?

I am trying to create when I run a UWP application.

I have the following code:

string documentsPath = Package.Current.InstalledLocation.Path; System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false); Task.Factory.StartNew(async () => { await Package.Current.InstalledLocation.CreateFolderAsync("Data"); mre.Set(); }); mre.WaitOne(); 

But the line:

 await Package.Current.InstalledLocation.CreateFolderAsync("Data"); 

produces the following error:

"Access denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}

Looking at this link: UWP file permissions , he points out the following:

When creating a new application, you can access the following default location file system:

Application installation directory. The folder in which your application is installed in the user system.

There are two main ways to access files and folders in applications to set a directory:

You can get a StorageFolder that represents your application installation directory, for example:

 Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation 

So, I would think that my code would work. So my question is: how to create a folder using the UWP application?

+5
source share
2 answers

You cannot create a folder in InstalledLocation, MSDN :

... The application installation directory is a read-only place ...

Try using the local folder instead:

ApplicationData.Current.LocalFolder.CreateFolderAsync("Data");

+12
source

Use ApplicationData.Current.LocalFolder.Path , not Package.Current.InstalledLocation.Path

 string documentsPath = ApplicationData.Current.LocalFolder.Path; System.Threading.ManualResetEvent mre = new System.Threading.ManualResetEvent(false); Task.Factory.StartNew(async () => { await ApplicationData.Current.LocalFolder.CreateFolderAsync("Data"); mre.Set(); }); mre.WaitOne(); 

Package.Current.InstalledLocation.Path gives you a way where all of your code and resources work with the visual studio debugger, which is usually the debug folder for the source code. This is not available through the UWP API libraries that are commonly available in .NET applications (win32).

The UWP application has read / write access to the folder " C: \ Users \ {userprofile} \ AppData \ Local \ Packages \ {packagenameguid} \" . You can create the folder / files in this place at the runtime of applications.

+4
source

Source: https://habr.com/ru/post/1246875/


All Articles