SilverlightisolatedStorage:删除不起作用,截断留下空白

发布于 2024-08-16 18:19:56 字数 791 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南汐寒笙箫 2024-08-23 18:19:56

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

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

没有使用“截断”方法来工作,但现在不需要。

I've found a way to do this, not by closing but by disposing:

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

Didn't get the 'truncate' approach to work, but no need now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文