I'd like to write text content into a file that is located Assets folder, so I access file but I don't have access to write into it, my code is:
try {
//get the file
StorageFile storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///assets/test.txt"));
//try to write sring to it
await FileIO.WriteTextAsync(storageFile, "my string");
} catch (Exception ex) {
Debug.WriteLine("error: " + ex);
}
and I get the error:
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
error: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MyProject.MainPage.<overWriteHtmlSrcFile>d__6.MoveNext()
Have to mention that I need to change this file due app scenario, or maybe is there a way to create this file in public app folder and then move it in assets.
Files located into Assets folder are read only that's why you get this exception. Like you mentioned at the end, there is a method to create your file in a public location, write what you need into it and then just move the file in assets folder. It will be like:
try {
//create file in public folder
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync("test.txt", CreationCollisionOption.ReplaceExisting);
//write sring to created file
await FileIO.WriteTextAsync(sampleFile, htmlSrc);
//get asets folder
StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assetsFolder = await appInstalledFolder.GetFolderAsync("Assets");
//move file from public folder to assets
await sampleFile.MoveAsync(assetsFolder, "new_file_name.txt", NameCollisionOption.ReplaceExisting);
} catch (Exception ex) {
Debug.WriteLine("error: " + ex);
}
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