我試圖將RichEditBox的內容保存到我的應用臨時文件夾,但是我無法使其工作。將RichText文檔保存到Windows應用商店應用中的臨時文件夾
這裏是工作的代碼,將文件保存到磁盤上,通過保存文件選擇器:
// [code for savePicker. Not relevant because that all works fine]
StorageFile file = await savePicker.PickSaveFileAsync();
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
editor.Document.SaveToStream(TextGetOptions.FormatRtf, stream);
這裏的工作代碼到一個txt文件保存到臨時存儲
StorageFolder temp = ApplicationData.Current.TemporaryFolder;
StorageFile file = await temp.CreateFileAsync("temp.txt",
CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "some text");
所以當我結合這些將RTF內容保存到臨時文件夾,這是我寫的:
StorageFolder temp = ApplicationData.Current.TemporaryFolder;
StorageFile file = await temp.CreateFileAsync("temp.rtf",
CreationCollisionOption.ReplaceExisting);
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
editor.Document.SaveToStream(TextGetOptions.FormatRtf, stream);
This不起作用。我在第二行StorageFile file = etc
上收到訪問被拒絕錯誤(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
。然而,這段代碼在上面的第二個塊中執行得很好。看來,當我跟着它與file.OpenAsync它會引發錯誤。有人可以在這裏指出我正確的方向嗎?這與await
有關嗎?
編輯:我已經接受並由Damir Arh提出答案,因爲它是對這個問題的正確解決方案。我的解決方法爲我解決了這個問題,但Damir Arh的回答解決了問題的根本原因,當然總是更好。
謝謝,我想我是基於這個想出來的:這是自動保存功能的一部分,每90秒,因爲我不想每次測試都要等待90秒,所以我將它設置爲在應用程序啓動後1秒開始,這當然也意味着它每秒發射一次! – roryok