0

IsloatedStorage在 Silverlight 应用程序中使用来记录客户端上的信息,并添加了一个清除日志文件的功能。但是,我尝试过的两种方法都有问题:

方法一:使用

IsolatedStorageFile.DeleteFile("log.log");

结果:此操作失败并返回“[IsolatedStorage_DeleteFile]”错误(无其他信息)。该功能在测试文件上运行良好,例如DeleteFile("test.txt"),但拒绝删除日志。我虽然可能正在使用日志,并试图关闭它

IsolatedStorageFileStream.close()

但这会返回一个不同的错误“[IsolatedStorage_StoreNotOpen]”。我知道它是打开的,因为上一行代码成功记录了一条消息。

方法二:使用截断文件模式重新打开日志文件,

_storageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, _storageFile);

根据 MSDN,Truncate “指定操作系统应打开现有文件。一旦打开,文件应被截断,使其大小为零字节。” 但是,它会打开我的日志文件并用空格填充它!文件大小保持不变,下一条日志消息附加到所有空间的末尾。

4

1 回答 1

0

我找到了一种方法来做到这一点,不是通过关闭而是通过处置:

IsolatedStreamWriter.Dispose();                       
IsolatedStorageFile.DeleteFile("log.log");
IsolatedStorageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite, _storageFile);

没有得到“截断”的工作方法,但现在不需要。

于 2010-01-07T15:40:55.157 回答