I am trying to create when I run my 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");
throws the following error:
"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}
Looking at this link: UWP File access permissions it states the following:
When you create a new app, you can access the following file system locations by default:
Application install directory. The folder where your app is installed on the user’s system.
There are two primary ways to access files and folders in your app’s install directory:
You can retrieve a StorageFolder that represents your app's install directory, like this:
Windows.Storage.StorageFolder installedLocation = Windows.ApplicationModel.Package.Current.InstalledLocation
So I would have thought my code would work. So my question is how do I create a folder using a UWP application?
When a UWP app is installed, several folders are created under c:\users<user name>\AppData\Local\Packages<app package identifier>\ to store, among other things, the app's local, roaming, and temporary files.
The picker uses a single, unified interface to let the user pick files and folders from the file system or from other apps. Files picked from other apps are like files from the file system: they are returned as StorageFile objects. In general, your app can operate on them in the same ways as other objects.
You can't create folder in InstalledLocation, MSDN:
...The app's install directory is a read-only location...
Try to use local folder instead:
ApplicationData.Current.LocalFolder.CreateFolderAsync("Data");
Use ApplicationData.Current.LocalFolder.Path rather than 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 the path where all your code and resource running using the visual studio debugger which is typically the debug folder for source code. This is not accessible via UWP API libraries which is typically available in .Net applications (win32).
UWP app have read/write access to folder "C:\Users\{userprofile}\AppData\Local\Packages\{packagenameguid}\" You can create folder/files at this location at runtime of applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With