【问题标题】:Operation not permitted on IsolatedStorageFileStream when I try to use a StreamWriter当我尝试使用 StreamWriter 时,IsolatedStorageFileStream 上不允许操作
【发布时间】:2013-07-09 02:19:40
【问题描述】:

当我尝试使用此代码时遇到问题:

using (IsolatedStorageFile myISF = IsolatedStorageFile.GetUserStoreForApplication())         
{
    try
    {
        object _readLock = new object();
        lock (_readLock)
        {
            var myFS = myISF.CreateFile(reply_FileName.Replace(" ", "_"));

            StreamWriter mySW = new StreamWriter(myFS);
            mySW.WriteLine(jsonToSave);
            mySW.Close();
        }
    }
    catch (IsolatedStorageException ex) { MessageBox.Show(ex.Message); }                
}

我已经尝试StreamWriter 也使用了,但问题没有解决。我有 2 页,在我使用的第一页上:

using (myFS = new IsolatedStorageFileStream(Forms_path + form_ID + ".json",
                  FileMode.Create, FileAccess.Write, myISF))
{
    using (StreamWriter mySW = new StreamWriter(myFS))
    {
        mySW.WriteLine(json);
        mySW.Close();
    }
    myFS.Dispose();
}
myFS.Close();

效果很好。只是第二个代码有问题。我尝试了很多变体,但都不是很好。

编辑:当问题发生时,我在输出窗口中收到此消息:

“System.IO.IsolatedStorage.IsolatedStorageException”类型的第一次机会异常发生在 mscorlib.ni.dll 中

EDIT2: 问题是我的文件名。我在文件名中添加了日期,“:”和“/”符号引起了麻烦。我觉得很可怜……

【问题讨论】:

  • 在第二个示例中删除所有对DisposeClose 的调用。
  • 谢谢,但是还不行。

标签: windows-phone-8 windows-phone filestream isolatedstoragefile


【解决方案1】:

每次调用方法时都会创建锁定对象,因此它不会阻塞其他线程,因此会出现异常。 _readLock 变量应该是类中的静态字段。

【讨论】:

  • 有趣,尝试使用 OpenFile 和 FileMode.Create 而不是 CreateFile。
【解决方案2】:

问题是我的文件名。我在文件名中添加了日期,“:”和“/”符号引起了麻烦。我觉得很可怜……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2021-04-01
    相关资源
    最近更新 更多