I’m trying to write some code to write to a text file. I had code working… but today (without changes) it started generating an “Access Denied” error. I was writing to the LocalFolder (Windows.Storage.ApplicationData.Current.LocalFolder).
Do I have to declare in the manifest that I want to save files in the LocalStorage? I know I have to for My Documents, or am I missing something? Here’s my sample method that illustrates how I’m trying to write out to a file:
''' <summary>
''' Writes all of the text to the specified file in one of the specified safe storage folders.
''' </summary>
''' <param name="text">The text to write.</param>
''' <param name="append">Whether or not to append the data or overwrite what is in the file.</param>
''' <param name="fileName">The name of the file to write the data to.</param>
''' <param name="safeFolder">The safe storage folder that should be written to. These folders are isolated for the application to use
''' and do not require additional manifest permissions.</param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Async Function WriteAllText(text As String, append As Boolean, fileName As String, safeFolder As SafeStorageFolder) As Task
Dim folder As Windows.Storage.StorageFolder
Select Case safeFolder
Case SafeStorageFolder.Local
folder = Windows.Storage.ApplicationData.Current.LocalFolder
Case SafeStorageFolder.Roaming
folder = Windows.Storage.ApplicationData.Current.RoamingFolder
Case SafeStorageFolder.Temp
folder = Windows.Storage.ApplicationData.Current.TemporaryFolder
Case Else
folder = Windows.Storage.ApplicationData.Current.LocalFolder
End Select
Dim sf As StorageFile
If append = True Then
sf = Await folder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.OpenIfExists)
Else
sf = Await folder.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.ReplaceExisting)
End If
' WriteTextAsync will always overwrite the file even if the existing file has been opened. We'll use
' AppendTextAsync here, the above CreateFileAsync will handle whether the file has been truncated or not.
Await FileIO.AppendTextAsync(sf, text)
End Function
I used the same code logic you have in a test project and it works fine for me. I didn’t go through all of the append/don’t append paths, but I was able to write to each folder.
Have you pulled the file path from the folder object you set in the select and double-check that you can open it in File Explorer?