";不允许对IsolatedStorageFileStream执行操作”;在写入文件时

本文关键字:文件 执行 不允许 quot IsolatedStorageFileStream 操作 | 更新日期: 2023-09-27 18:22:03

我正试图使用二进制序列化和WPPerfLab中的一些助手来序列化一个对象,我在这一行中得到了错误:

using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(FileName, FileMode.Create, myIsolatedStorage))

这是我正在做的事情的一小段。

    using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists(FileName))
            {
                myIsolatedStorage.DeleteFile(FileName);
            }                
            using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(FileName, FileMode.Create, myIsolatedStorage))
            {                                        
                new BinarySerializationHelper().Serialize(fileStream, object);
            }
    }

经过谷歌搜索,我发现这可能是一个与流相关的错误,但我很确定没有其他流打开了我试图写入的文件(名称也是随机的!)。

那么,我该如何解决这个问题呢?

";不允许对IsolatedStorageFileStream执行操作”;在写入文件时

我找到了你的错误兄弟。实际上,错误的原因不是IsolatedStorageFileStream,而是看起来很无辜的字符串FileName。

我想你正在生成FIleName作为

string FileName="some_random_string"+DateTime.Now.ToString("hh:mm");

或者使用任何格式的日期时间。在这行上放置一个断点。您会发现FileName值包含字符":"。。。。这是非常非常糟糕的命名。因此出现了错误。请尝试以其他方式命名文件。

但是这个IsolatedStorageFileStream构造函数让我非常头疼。所以我用另一种方式。我正在使用IsolateStorage流的方法打开文件。查看代码

 using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (myIsolatedStorage.FileExists(FileName))
        {
            myIsolatedStorage.DeleteFile(FileName);
        }                
        using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(FileName, System.IO.FileMode.CreateNew))
        {                                        
            new BinarySerializationHelper().Serialize(fileStream, object);
        }
}