Reputation: 1204
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
Upvotes: 4
Views: 3181
Reputation: 93
Try This
public static async void WriteTrace()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFolder LogFolder = await localFolder.CreateFolderAsync("LogFiles", CreationCollisionOption.OpenIfExists);
}
Upvotes: 1
Reputation: 12019
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();
Upvotes: 4
Reputation: 2265
Every application runs under (by) a specific user and inherit it's privileges, authorities, limitations and ... so The User that your app runs under it, haven't enough privilege and can't create a folder SO you can:
Upvotes: 0
Reputation: 1023
Run your application with Administrative privileges and it should be done.
Upvotes: 0