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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With