Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new directory in a Windows 10 Universal App UWP

I am attempting to create a directory using this method that fires after a button press in app, as well as add a file to it:

DirectoryInfo d = new DirectoryInfo(@"..\\newFolder\\");
FileInfo f = new FileInfo(@"..\\newFolder\\foo.txt");
if (!d.Exists)
{
    d.Create();
}
if (!f.Exists)
{
    f.Create().Dispose();
}

Here's the error that is producded in my Universal App as a result of doing this:

An exception of type 'System.UnauthorizedAccessException' 
occurred in System.IO.FileSystem.dll but was not handled in user code

Additional information: Access to the path
 'C:\Users\[username]\Documents\MyApp\bin\x86\Debug\AppX\newFolder' is denied.

Any one familiar with this error or know of any suggestions?

EDIT In addition to the below, this is a Great resource for working with file systems in the Windows 10 Universal App environment: File access and permissions (Windows Runtime apps) https://msdn.microsoft.com/en-us/library/windows/apps/xaml/Hh758325.aspx

like image 348
greg Avatar asked Nov 15 '25 14:11

greg


1 Answers

The problem is that you are trying to create a file inside your app's install folder, and that is not allowed for Universal Apps. You can only create folders inside your local data folder.

Try this:

using Windows.Storage;

var localRoot = ApplicationData.Current.LocalFolder.Path;
DirectoryInfo d = new DirectoryInfo(localRoot + "\\test");
if (!d.Exists)
  d.Create();
like image 159
Peter Torr - MSFT Avatar answered Nov 17 '25 07:11

Peter Torr - MSFT