SilverlightisolatedStorage:删除不起作用,截断留下空白
我在 Silverlight 应用程序中使用 IsloatedStorage
来记录客户端上的信息,并添加了一个清除日志文件的函数。但是,我尝试的两种方法都遇到了问题:
方法一:使用
IsolatedStorageFile.DeleteFile("log.log");
结果:此操作失败并返回“[IsolatedStorage_DeleteFile]”错误(无其他信息)。该函数在测试文件上运行良好,例如DeleteFile("test.txt")
,但拒绝删除日志。我认为日志可能正在被使用,并尝试将其关闭
IsolatedStorageFileStream.close()
但这会返回不同的错误“[IsolatedStorage_StoreNotOpen]”。我知道它已打开,因为前一行代码成功记录了一条消息。
方法二:使用Truncate file模式重新打开日志文件,
_storageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, _storageFile);
根据MSDN,Truncate “指定操作系统应打开一个现有文件。一旦打开,该文件应被截断,使其大小为零字节。” 但是,它打开我的日志文件并用空格填充它!文件大小保持不变,下一条日志消息将附加到所有空间的末尾。
I'm using IsloatedStorage
in a Silverlight app to log information on the client, and I added a function to clear the log file. However, I have had problems with the two approaches I tried:
Approach one: use
IsolatedStorageFile.DeleteFile("log.log");
Result: This fails and returns an "[IsolatedStorage_DeleteFile]" error (No other info). The function works fine on test files, e.g. DeleteFile("test.txt")
, but refuses to delete the log. I though that perhaps the log is being used, and tried to close it with
IsolatedStorageFileStream.close()
But this returns a different error "[IsolatedStorage_StoreNotOpen]". I know it is open as the previous line of code successfully logs a message.
Approach Two: Reopen the log file using the Truncate file mode,
_storageFileStream = new IsolatedStorageFileStream(logfilename, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite, _storageFile);
According to MSDN, Truncate "Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes." However, it opens my log file and fills it with blank space! The filesize is left identical, the next log message is appended to the end of all of the space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种方法来做到这一点,不是通过关闭,而是通过处置:
没有使用“截断”方法来工作,但现在不需要。
I've found a way to do this, not by closing but by disposing:
Didn't get the 'truncate' approach to work, but no need now.