简体   繁体   English

将RichText文档保存到Windows Store应用程序中的temp文件夹中

[英]Saving a RichText document to the temp folder in a Windows Store App

I'm trying to save the content of a RichEditBox to my apps temporary folder, but I can't get it to work. 我正在尝试将RichEditBox的内容保存到我的应用程序临时文件夹中,但无法正常工作。

Here's the working code to save the file to disk, through the Save File picker: 这是通过“保存文件”选择器将文件保存到磁盘的工作代码:

// [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);

Here's the working code to save a txt file to the temp storage 这是将txt文件保存到临时存储的工作代码

StorageFolder temp = ApplicationData.Current.TemporaryFolder;
StorageFile file = await temp.CreateFileAsync("temp.txt", 
                         CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(file, "some text");

So when I combined these to save RTF content to the temp folder, this is what I wrote: 因此,当我结合使用它们将RTF内容保存到temp文件夹时,这就是我写的内容:

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 doesn't work. 这行不通。 I get an Access Denied error (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) on the second line StorageFile file = etc . 我在第二行StorageFile file = etc上收到“拒绝访问”错误(Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) However this same line of code executes fine in the second block above. 但是,同一行代码在上面的第二个块中可以很好地执行。 It seems that when I follow it with file.OpenAsync it throws an error. 似乎当我将其与file.OpenAsync一起使用时,它将引发错误。 Can someone point me in the right direction here? 有人可以在这里指出正确的方向吗? Is it something to do with await ? await吗?

EDIT: I've accepted and upvoted the answer by Damir Arh, as it is the correct fix to this issue. 编辑:我已经接受达米尔·阿赫(Damir Arh)的投票,并对其进行了投票,因为这是针对此问题的正确解决方案。 My workaround solved the issue for me, but Damir Arh's answer addresses the root cause of the issue which is always better of course. 我的解决方法为我解决了这个问题,但是Damir Arh的回答解决了问题的根本原因,当然总会更好。

This block of code should work just fine; 此代码块应该可以正常工作; I even tested it, just to be sure: 我什至进行了测试,只是为了确保:

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);

You have incorrectly identified, why it was failing. 您错误地确定了失败原因。 CreateFileAsync can't fail because it is followed by OpenAsync ; CreateFileAsync不会失败,因为它后面是OpenAsync the latter didn't even start executing when it failed. 后者甚至在失败时也没有开始执行。

The most probable reason was that you already had it open from a Stream that you didn't close properly. 最可能的原因是您已经从不正确关闭的Stream中打开了它。 This can still happen even with the code that you have posted in the answer. 即使您在答案中发布了代码,这仍然可能发生。

I would suggest you use CreationCollisionOption.GenerateUniqueName instead of CreationCollisionOption.ReplaceExisting . 我建议您使用CreationCollisionOption.GenerateUniqueName代替CreationCollisionOption.ReplaceExisting This way a file with a different name will be created if for some reason it can't be with the original file name. 这样,如果由于某种原因不能使用原始文件名,则将创建一个具有不同名称的文件。

Also make sure you're properly closing the stream once you're done with writing. 完成写操作后,还要确保已正确关闭流。 Since IRandomAccessStream implements IDisposable , you should always call Dispose on it when you don't need it any more. 由于IRandomAccessStream实现了IDisposable ,因此在不再需要它时,应始终对其调用Dispose Or even better: put it in a using block which will do this for you. 甚至更好:将其放在using块中,它将为您完成此操作。

Here's the code with these two changes applied: 这是应用了这两个更改的代码:

StorageFolder temp = ApplicationData.Current.TemporaryFolder;
StorageFile file = await temp.CreateFileAsync("temp.rtf",
                            CreationCollisionOption.GenerateUniqueName);
using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    Editor.Document.SaveToStream(TextGetOptions.FormatRtf, stream);
    await stream.FlushAsync();
}

I've figured out a workaround: 我想出了一种解决方法:

StorageFolder temp = ApplicationData.Current.TemporaryFolder;
StorageFile file = await temp.CreateFileAsync("temp.rtf", 
                         CreationCollisionOption.ReplaceExisting);
string rtfcontent = "";
editor.Document.GetText(TextGetOptions.FormatRtf, out rtfcontent);
await FileIO.WriteTextAsync(file, rtfcontent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM