(转)Windows Phone7隔离存储空间
原文地址:http://www.cnblogs.com/xieLongBao/archive/2012/02/28/2372311.html
Windows Phone7隔离存储空间
隔离存储空间:
- 目录操作
- 文件操作
- 应用程序配置信息
隔离存储空间的概念:所有文件IO操作被限制在隔离存储空间里面,在隔离存储空间里面可以增删改目录和文件,在隔离存储空间里面可以存储程序配置信息
重要的类:
- IsolatedStorageFile用于操控隔离存储空间里面的目录及文件,
- IsolatedStorageFileStream用于读写操控隔离存储空间里面的流
- IsolatedStorageFileSettings用于存储程序配置信息的Dictionary
配额管理:
- windows phone下的隔离存储空间没有配额的限制
| usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Animation;usingSystem.Windows.Shapes;usingMicrosoft.Phone.Controls;usingSystem.IO.IsolatedStorage;usingSystem.IO;namespaceIsolatedStorage{    publicpartialclassMainPage : PhoneApplicationPage    {        // Constructor        publicMainPage()        {            InitializeComponent();        }        privateconststringfoldername = "temp1";        privateconststringfilename = foldername + "/address.txt";        privateconststringsettingname = "sname";        /// <summary>        /// 创建文件夹        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton1_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                file.CreateDirectory(foldername);            }        }        /// <summary>        /// 检查文件夹是否存在        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton2_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                if(file.DirectoryExists(foldername))                {                    MessageBox.Show("已存在");                }                else                {                    MessageBox.Show("不存在");                }            }        }        /// <summary>        /// 删除目录        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton3_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                file.DeleteDirectory(foldername);            }        }        /// <summary>        /// 创建文件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton4_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                IsolatedStorageFileStream stream = file.CreateFile(filename);                stream.Close();            }        }        /// <summary>        /// 检查文件是否存在        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton5_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                if(file.FileExists(filename))                {                    MessageBox.Show("已存在"+ filename);                }                else                {                    MessageBox.Show("不存在");                }            }        }        /// <summary>        /// 删除文件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton6_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                file.DeleteFile(filename);            }        }        /// <summary>        /// 向文件里增加内容        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton7_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                using(IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))                {                    StreamWriter writer = newStreamWriter(stream);                    writer.WriteLine(textBox1.Text);                    writer.Close();                    textBox1.Text = "";                }                           }        }        /// <summary>        /// 读取文件内容        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton8_Click(objectsender, RoutedEventArgs e)        {            using(IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())            {                using(IsolatedStorageFileStream stream = file.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite))                {                  using(StreamReader  reader=newStreamReader (stream))                  {                      textBox1.Text = reader.ReadToEnd();                  }                }            }        }        /// <summary>        /// 程序配置信息保存        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton9_Click(objectsender, RoutedEventArgs e)        {            IsolatedStorageSettings.ApplicationSettings[settingname] = textBox2.Text;            IsolatedStorageSettings.ApplicationSettings.Save();            textBox2.Text = "";        }        /// <summary>        /// 程序配置信息读取        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        privatevoidbutton10_Click(objectsender, RoutedEventArgs e)        {            if(IsolatedStorageSettings.ApplicationSettings.Contains(settingname))            {                textBox2.Text = IsolatedStorageSettings.ApplicationSettings[settingname].ToString();            }        }    }} | 
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号