zoukankan      html  css  js  c++  java
  • Operation not permitted on IsolatedStorageFileStream.

      我在使用IsolatedStorageFileStream时遇到了一个很顽固的异常“Operation not permitted on IsolatedStorageFileStream.”至今没办法找到。大侠求助

    异常截图

    代码如下

    Student.cs

       public class Student
        {
            public string EMail { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }

            public Student(string FirstName, string LastName)
            {
                this.FirstName = FirstName;
                this.LastName = LastName;
                this.EMail = FirstName + "@" + LastName + ".com";
            }

            public Student(XElement xElement)
            {
                this.FirstName = xElement.Attribute("FirstName").Value;
                this.LastName = xElement.Attribute("LastName").Value;
                this.EMail = xElement.Attribute("EMail").Value;
            }

            public XElement Information
            {
                get
                {
                    return new XElement("Student", new XAttribute("EMail", EMail), new XElement("FirstName", FirstName), new XElement("LastName", LastName));
                }
            }
        }

    StudentList .cs

      public class StudentList : List<Student>
        {
            public void Load(string strXMLFile)
            {
                IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
                XDocument doc = null;

                IsolatedStorageFileStream isfStream = null;

                if (isfData.FileExists(strXMLFile))
                {
                    isfStream = new IsolatedStorageFileStream(strXMLFile, System.IO.FileMode.Open,FileAccess.Write, isfData);
                    doc = XDocument.Load(isfStream);
                    isfStream.Close();
                }

                else
                {
                    doc = XDocument.Load(strXMLFile);
                    isfStream = new IsolatedStorageFileStream(strXMLFile, System.IO.FileMode.CreateNew, FileAccess.Write, isfData);
                    doc.Save(isfStream);
                    isfStream.Close();
                }

                var vStudent = from s in doc.Descendants("Student") select new Student(s);
                this.Clear();
                AddRange(vStudent);
            }

            public void Save(string strXMLFile)
            {
                try
                {
                    XElement xml = new XElement("Students", from p in this select p.Information);
                    IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(
                        strXMLFile,
                        System.IO.FileMode.Open,
                        IsolatedStorageFile.GetUserStoreForApplication());
                    xml.Save(isfStream);
                    isfStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

    Students.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <Students>
      <Student EMail="Woo@Jianqiang.com">
        <FirstName>Woo</FirstName>
        <LastName>Jianqiang</LastName>
      </Student>
      <Student EMail="Whitcomb@Donald.com">
        <FirstName>Whitcomb</FirstName>
        <LastName>Donald</LastName>
      </Student>
      <Student EMail="Kadi@Wadad.com">
        <FirstName>Kadi</FirstName>
        <LastName>Wadad</LastName>
      </Student>
    </Students>

    Mainpage.xaml

    <ListBox x:Name="lbData"></ListBox>

    Mainpage.cs

        public partial class MainPage : PhoneApplicationPage
        {
            StudentList data =null;
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }

            private void loadData() {
                data = new StudentList();
                data.Load("Model/Students.xml");
                lbData.ItemsSource = data;
            }

            private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
            {
                loadData();
            }

  • 相关阅读:
    样式问题
    布局
    通用模板实现可变参数函数
    使用单例模式实习string类
    动态生成一维数组和二维数组
    自定义的类传数据到主窗口控件上的方法
    使用TableView
    G480折腾上了黑苹果,完美了,哈哈
    error C2383: 此符号中不允许有默认参数
    动态链接库的隐式动态链接和显示动态链接
  • 原文地址:https://www.cnblogs.com/salam/p/1918029.html
Copyright © 2011-2022 走看看