【发布时间】:2016-10-02 09:22:32
【问题描述】:
我正在尝试将一堆文件保存到一个文件夹中。该文件夹已经存在,但我想完全删除它及其内容并将新文件保存到该文件夹中。我正在使用以下代码:
Project project; //This is a parameter handed in when the method is called
List<InkStrokeContainer> containers; //This is a parameter handed in when the method is called
var projectFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync(project.Id.ToString());
var slidesFolder = await projectFolder.CreateFolderAsync("Slides", CreationCollisionOption.ReplaceExisting);
StorageFile file;
IRandomAccessStream stream;
for (int i = 0; i < containers.Count; i++)
{
if (containers[i].GetStrokes().Count > 0)
{
file = await slidesFolder.CreateFileAsync($"{i.ToString()}.jpg", CreationCollisionOption.ReplaceExisting);
stream = await file.OpenAsync(FileAccessMode.ReadWrite);
await containers[i].SaveAsync(stream);
await stream.FlushAsync();
}
}
但是,我第二次尝试这样做时遇到了异常:
当文件已存在时无法创建该文件。 (HRESULT 异常:0x800700B7)
请注意,我可以保存一次,它会正确替换文件夹。只有当我第二次尝试这样做时它才会失败。它应该在碰撞时替换现有项目,所以我怀疑我没有正确释放某些东西,但我不知道是什么。
【问题讨论】: