我正在編寫一個Windows Phone 8.1(WINRT XAML)應用程序。如何關閉打開的StorageFile?
在方法1中,我打開一個StorageFile,並在方法2中寫入同一個文件中的。但是在寫入文件時顯示錯誤。
- 如何打開和閱讀內容後關閉此文件?
那麼如何在寫入方法2之後再次關閉此文件?
private async Task<bool> Method1(int workOutID) { string filename = "WorkoutOfflineDataFile.txt"; StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder; StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); string JSONFromFile = await FileIO.ReadTextAsync(StorageFileObject); } private async void Method2() { string filename = "WorkoutOfflineDataFile.txt"; StorageFolder LocalStorageFolderObject = ApplicationData.Current.LocalFolder; StorageFile StorageFileObject = await LocalStorageFolderObject.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists); await FileIO.WriteTextAsync(StorageFileObject, JSonData_ToSave); }
ERROR:
Exception thrown: 'System.UnauthorizedAccessException' in mscorlib.ni.dll
Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
請將您的代碼粘貼到代碼塊中。現在它不可讀。 – Festyk
@Festyk,請立即檢查 –
您打算如何調用這些方法? –