I am trying to write code to write to a text file. I had code working ... but today (unchanged) it started to generate an "Access Denied" error. I wrote in LocalFolder (Windows.Storage.ApplicationData.Current.LocalFolder).
Should I declare in the manifest that I want to save the files in LocalStorage? Do I know that I should use my documents, or am I missing something? Here is my sample method that illustrates how I am trying to write 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
source share