9

I got Access is denied when trying to CreateFileAsync in InstalledLocation StorageFolder

StorageFolder storageFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await storageFolder.CreateFileAsync("fileNmae", Windows.Storage.CreationCollisionOption.ReplaceExisting);

Also I tried

var storageFolder = await StorageFolder.GetFolderFromPathAsync("ms-appx:///");

And got "value does not fall within the expected range"

I can go around and CreateFileAsync in Windows.Storage.ApplicationData.Current.LocalFolder then CopyAsync to InstalledLocation StorageFolder?

StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile file = await storageFolder.CreateFileAsync("fileName", Windows.Storage.CreationCollisionOption.ReplaceExisting);

StorageFolder installedLocationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var result = await file.CopyAsync(installedLocationFolder, "fileName", Windows.Storage.NameCollisionOption.ReplaceExisting);

but CreateFileAsync in InstalledLocation StorageFolder gives Access denied ? is that because of security reason or I'm coding something wrong here?

isa
  • 1,055
  • 16
  • 26

1 Answers1

15

The app's install directory is a read-only location. In addition, it would not be recommended that you write data files to the installed location. If you need to store data this is for the application's use only, you should use

StorageFolder localFolder = ApplicationData.Current.LocalFolder;

or

Windows.Storage.StorageFolder temporaryFolder = ApplicationData.Current.TemporaryFolder;

depending on the lifetime of the data.

Jeff Brand
  • 5,623
  • 1
  • 23
  • 22
  • If I'm not wrong the install directory and its sub directories are read-only locations. – isa Sep 03 '12 at 16:20