Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a folder in a UWP application?

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?

like image 306
User1 Avatar asked Apr 11 '16 13:04

User1


People also ask

Where is UWP app data location?

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.

What is open file picker?

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.


2 Answers

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

like image 66
M. Pipal Avatar answered Oct 23 '22 00:10

M. Pipal


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.

like image 20
PazDev Avatar answered Oct 23 '22 01:10

PazDev