FileIO.AppendTextAsync实际上是覆盖

FileIO.AppendTextAsync is actually overwriting

我正在使用 FileIOLocalStorage 文件中附加 Json 数据。

public static async Task AppendToJsonLocalStorage<T>(string filename, T objectToWrite) where T : new()
{
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;

    StorageFile saveFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
    var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
    await FileIO.AppendTextAsync(saveFile, contentsToWriteToFile);
}

AppendTextAsync 应该只在现有文件的末尾添加新文本,赖特?

因为当我使用文本编辑器在我的文件资源管理器中检查文件时,它总是覆盖其中以前的文本。

创建文件时使用 CreationCollisionOption.OpenIfExists 而不是 CreationCollisionOption.ReplaceExisting

StorageFile saveFile = await localFolder.CreateFileAsync(filename, CreationCollisionOption.OpenIfExists);
var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite);
await FileIO.AppendTextAsync(saveFile, contentsToWriteToFile);
顾名思义,

ReplaceExisting 替换任何现有文件。详情请参考docs