Reputation: 18789
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?
Upvotes: 7
Views: 10638
Reputation: 1027
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.
Upvotes: 5