【问题标题】:Silverlight IsolatedStorage: Delete not working, Truncate leaving blank spaceSilverlight 隔离存储:删除不起作用,截断留下空白空间
【发布时间】:2010-01-07 12:02:22
【问题描述】:

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

方法一:使用

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

【问题讨论】:

    标签: silverlight isolatedstorage truncate


    【解决方案1】:

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

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

    没有让“截断”方法起作用,但现在不需要。

    【讨论】:

    • 请注意,如果您为 Windows Phone 7 应用程序使用独立存储,则无论如何都不支持 FileMode.Truncate。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多