在查看了@Igor 留下的评论后,我想出了使用以下代码解决我的问题的方法:
IRandomAccessStream sessionRandomAccessFile1 = await this.m_StorageFile1.OpenAsync(FileAccessMode.Read);
IRandomAccessStream sessionRandomAccessFile2 = await this.m_StorageFile2.OpenAsync(FileAccessMode.Read);
if (sessionRandomAccessFile1.Size == 0)
{
await FileIO.AppendLinesAsync(m_StorageFile1, lines);
}
else if (sessionRandomAccessFile1.Size < 5000000)
{
if (sessionRandomAccessFile1.Size > 4999900 )
{
await m_StorageFile2.DeleteAsync();
AssignLocalFile();
}
await FileIO.AppendLinesAsync(m_StorageFile1, lines);
}
else if (sessionRandomAccessFile2.Size < 5000000)
{
if (sessionRandomAccessFile1.Size > 5000000 && sessionRandomAccessFile2.Size > 4999900)
{
await m_StorageFile1.DeleteAsync();
AssignLocalFile();
}
await FileIO.AppendLinesAsync(m_StorageFile2, lines);
}
在方法assignlocalfile中,我只是在删除它们后创建新文件:
private async void AssignLocalFile()
{
m_StorageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("VakantieApplicatie", CreationCollisionOption.OpenIfExists);
m_StorageFile1 = await m_StorageFolder.CreateFileAsync(m_Name.Replace(" ", "_") + "1.log",
CreationCollisionOption.OpenIfExists);
m_StorageFile2 = await m_StorageFolder.CreateFileAsync(m_Name.Replace(" ", "_") + "2.log",
CreationCollisionOption.OpenIfExists);
}
这样,我的最大总大小为 10MB,当一个 5MB 的文件已满时,它将切换到另一个而不删除前一个文件,因为您在搜索错误时可能需要它(在记录的情况下),当其他 5 MB 已满,它将清除第一个并再次开始在那里写入,因为在 5 MB 的错误记录之后,我认为您将不再需要该数据。