WP7隔离存储空间

1.需要用到的类

IsolatedStorageFile用于操作隔离存储空间里面的目录以及文件

IsolatedStorageFileStream用于读写操作隔离存储空间里面的文件流

IsolatedStorageSettings用于存储程序配置信息的Dictionary

2.引用命名空间

using System.IO.IsolatedStorage;

using System.IO;

 

3.目录操作

   新增目录

 

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               file.CreateDirectory(FolderName);

            }

 

   检查目录是否存在

 

 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

                if(file.DirectoryExists(FolderName))

                {

                    MessageBox.Show(FolderName+" 已经存在");

                }

                else 

                {

                    MessageBox.Show(FolderName+" 不存在");

                }

            }

删除目录

 

 

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               file.DeleteDirectory(FolderName);

            }

 

4.文件操作

   新增文件

 

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               FileStream fileStream=file.CreateFile(FileName);

               fileStream.Close();

            }

检查文件是否存在

 

 

 using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

                if(file.FileExists(FileName))

                {

                    MessageBox.Show(FileName+" 已经存在");

                }

                else 

                {

                    MessageBox.Show(FileName+" 不存在");

                }

            }

删除文件

 

 

         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               file.DeleteFile(FileName);

            }

 

5.文件读写

写文件

 

            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               using(IsolatedStorageFileStream fileStream=file.OpenFile(FileName,FileMode.Open,FileAccess.ReadWrite))

               {

                   StreamWriter streamWrite=new StreamWriter(fileStream);

                   streamWrite.WriteLine(MegTextBox.Text);

                   streamWrite.Close();

               }

            }

读文件

 

 

         using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())

            {

               using(IsolatedStorageFileStream fileStream=file.OpenFile(FileName,FileMode.Open,FileAccess.ReadWrite))

               {

                   StreamReader streamReader=new StreamReader(fileStream);

                   MegTextBox.Text=streamReader.ReadToEnd();

                   streamReader.Close();

               }

            }

 

6.应用程序配置信息

写配置

 

            IsolatedStorageSettings.ApplicationSettings[setting]=SetTextBox.Text;

            IsolatedStorageSettings.ApplicationSettings.Save();

读配置

 

 

            if(IsolatedStorageSettings.ApplicationSettings.Contains(setting))

            {

                SetTextBox.Text=IsolatedStorageSettings.ApplicationSettings[setting] as string;

            }

 

 

程序运行如图WP7隔离存储空间

你可能感兴趣的:(wp7)