WP8中的IsolatedStorageFileStream上不允许操作

时间:2013-11-19 15:06:53

标签: c# exception windows-phone-8 isolatedstorage

我见过很多类似的问题,我已经尝试了所有的解决方案,但似乎没有一个对我有用。

这就是我现在所拥有的:

        private readonly object _lock = new object();
        List<DataModel> dataList = new List<DataModel>();

        lock (_lock)
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                try
                {
                    using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                            dataList = (List<DataModel>)serializer.Deserialize(stream);
                    }
                }
                catch (IsolatedStorageException e) { e.ToString(); }
            }
        }

错误发生在using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))行上。

我从其他解决方案中推断出,这个错误主要发生在我想写入文件而其他人正在阅读同时执行该代码块的时候,我最终锁定文件。

如果存在lockFileAccess.ReadWriteFileShare.ReadWrite语句,我很确定还有其他事情会抛出异常。

我的问题是什么可以抛出异常(IsolatedStorageException),我该如何处理它?

这个没有InnerException

编辑:根据Kookiz的建议,我将包含此代码行

首先,我创建我的.xml文件:

    public static void createDataXML()
    {
        List<DataModel> dataList = new List<DataModel>();

        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (myIsolatedStorage.FileExists("Data.xml"))
            { return; }
            using (IsolatedStorageFileStream stream = myIsolatedStorage.CreateFile("Data.xml"))
            {
                try
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                    serializer.Serialize(stream, dataList);
                }
                catch
                { }
            }
        }
    }

后来,我填写了这段代码:

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                    using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
                    {
                        serializer.Serialize(xmlWriter, dataList);
                    }
                }
            }

2 个答案:

答案 0 :(得分:1)

我知道这是一种不那么传统的做法,但这是一种方式。我试图做别的事,这就是结果。只需添加您需要的已知类型,这将起作用

[DataContractAttribute]
[KnownType (typeof(List<String>))]
public class SerializableObject
{
   [DataMember]
    public List<String> serFile { get; set; }
}
public static Object GetFile(String FileName)
{
    try
    {
      if (!IsolatedStorageFile.GetUserStoreForApplication().FileExists(FileName))
       {
         throw new System.ArgumentException("File Doesn't Exist In Isoloated Storage");
       }
    }
    catch { return null;  }

    Object ret = new Object();
    try
    {
      IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();

      IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
      IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(@"\" + FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


      SerializableObject serList = new SerializableObject();
      DataContractSerializer dsc = new DataContractSerializer(serList.GetType());
      ret = ((SerializableObject)dsc.ReadObject(fileStream)).serFile;
    }
    catch (Exception error) { throw new System.ArgumentException(error.Message); }
    return ret;
}

这里隐含的任务是您需要在SerializableObject实例中序列化它。如果您还需要该代码,请告诉我

修改

按照承诺,保存文件功能

  public static void SaveFile(String FileName, List<String> File)
    {
        try
        {
            if (FileName.Length < 1)
            {
                throw new System.ArgumentException("File Name Must Not Be Empty");
            }
            if (IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace <= 0)
            {
                throw new System.ArgumentException("Isolated Storage Out of Memory - Please free up space.");
            }
            if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(FileName))
            {
                throw new System.ArgumentException("File Already Exists - Please choose a unique name.");
            }
            if (File == null)
            {
                throw new System.ArgumentException("Cannot Save Null Files");
            }
        }
        catch (Exception e)
        {
            return;
        }
        try
        {
            SerializableObject so = new SerializableObject() { serFile = File };
            DataContractSerializer dsc = new DataContractSerializer(so.GetType());

            IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
            StreamWriter writer;

            writer = new StreamWriter(new IsolatedStorageFileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, file));

            dsc.WriteObject(writer.BaseStream, so);
        }
        catch (Exception error) { throw new System.ArgumentException(error.Message); }

    }

享受序列化!

答案 1 :(得分:0)

我亲爱的,虽然在IsolatedStorage上不允许操作的这个例外主要是由于另一个线程的ReadWrite,在你的情况下是由于一个简单的原因

当文件名不存在时,FileMode.Open会抛出异常

尝试使用FileMode.OpenOrCreate,它将像魅力一样工作

 List<DataModel> dataList = new List<DataModel>();
         using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
         {

             using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("Data.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
             {
                 XmlSerializer serializer = new XmlSerializer(typeof(List<DataModel>));
                 dataList = (List<DataModel>)serializer.Deserialize(stream);
             }

        }