How do I create a folder in a UWP application?

user1 picture user1 · Apr 11, 2016 · Viewed 8.5k times · Source

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?

Answer

M. Pipal picture M. Pipal · Apr 11, 2016

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");