6
votes

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?

2
try running the debugging application as administrator or visual studio as administratorClayton C
@Clayton C. Thanks for the suggestion but no such luck I'm affraid. Tried running Visual Studio as admin and my Remote Debugger program but still same errorUser1

2 Answers

15
votes

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

5
votes

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.