[索引页]
[×××]


稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)


作者:webabcd


介绍
Silverlight 2.0 数据的独立存储(Isolated Storage):
    IsolatedStorageFile - 操作 独立存储 的类
        IsolatedStorageFile.GetUserStoreForSite() - 按站点获取用户的独立存储
        IsolatedStorageFile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储
    IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表
        IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表
        IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表


在线DEMO
http://webabcd.blog.51cto.com/1787395/342779 


示例
IsolatedStorage.xaml
<UserControl x:Class="Silverlight20.Data.IsolatedStorage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel HorizontalAlignment="Left">
                <TextBox x:Name="txtMsg" Margin="5" />
                <TextBox x:Name="txtMsg2" Margin="5" />
                <Button x:Name="increase" Content="增加配额" Click="increase_Click" Margin="5" />
        </StackPanel>
</UserControl>
 
IsolatedStorage.xaml.cs
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Collections.Generic;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Linq;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Net;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Controls;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Documents;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Input;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Media;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Media.Animation;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.Windows.Shapes;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.IO.IsolatedStorage;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatedusing System.IO;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolatednamespace Silverlight20.Data
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated{
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated        public partial class IsolatedStorage : UserControl
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated        {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                public IsolatedStorage()
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        InitializeComponent();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // 演示 IsolatedStorageFile
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        Demo();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // 演示 IsolatedStorageSettings
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        Demo2();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// <summary>
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// 演示 IsolatedStorageFile
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// </summary>
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                void Demo()
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // Isolated Storage - 独立存储。一个虚拟文件系统
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // IsolatedStorageFile - 操作 独立存储 的类
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        //         IsolatedStorageFile.GetUserStoreForSite() - 按站点获取用户的独立存储
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        //         IsolatedStorageFile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // DirectoryExists(path) - 指定的路径是否存在
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // CreateDirectory(path) - 创建指定的路径
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // FileExists(path) - 指定的文件是否存在
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // CreateFile(path) - 创建指定的文件
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // GetDirectoryNames() - 获取根目录下的目录名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // GetFileNames()() - 获取根目录下的文件名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // GetDirectoryNames(path) - 获取指定目录下的目录名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // GetFileNames(path) - 获取指定目录下的文件名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // OpenFile() - 打开指定的文件。具体参数参看文档
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // DeleteFile(path) - 删除指定的文件
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // DeleteDirectory(path) - 删除指定的目录(要求目录存在,且目录内无内容)
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // Remove() - 关闭 IsolatedStorageFile 对象并移除独立存储内的全部内容
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 在根目录下创建指定的目录
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.DirectoryExists("Directory01"))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.CreateDirectory("Directory01");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.DirectoryExists("Directory02"))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.CreateDirectory("Directory02");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 创建指定的子目录
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                string subDirectory01 = System.IO.Path.Combine("Directory01", "SubDirectory01");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                string subDirectory02 = System.IO.Path.Combine("Directory01", "SubDirectory02");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.DirectoryExists(subDirectory01))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.CreateDirectory(subDirectory01);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.DirectoryExists(subDirectory02))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.CreateDirectory(subDirectory02);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 根目录下创建指定的文件
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.FileExists("RootFile.txt"))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream isfs = isf.CreateFile("RootFile01.txt");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isfs.Close();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 在指定的目录下创建指定的文件
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                string file01 = System.IO.Path.Combine(subDirectory01, "File01.txt");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                string file02 = System.IO.Path.Combine(subDirectory01, "File02.txt");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                string file03 = System.IO.Path.Combine(subDirectory01, "File03.xml");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.FileExists(file01))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // IsolatedStorageFileStream - 独立存储内的文件流。继承自 FileStream
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream isfs = isf.CreateFile(file01);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isfs.Close();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.FileExists(file02))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream isfs = isf.CreateFile(file02);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isfs.Close();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (!isf.FileExists(file03))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream isfs = isf.CreateFile(file03);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isfs.Close();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                txtMsg.Text += "根目录下的目录列表:\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 获取根目录下的目录名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                foreach (string directoryName in isf.GetDirectoryNames())
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += directoryName + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                txtMsg.Text += "根目录下的文件列表:\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 获取根目录下的文件名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                foreach (string fileName in isf.GetFileNames())
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += fileName + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                txtMsg.Text += "目录 Directory01 下的目录列表:\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 获取 Directory01 目录下的目录名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                foreach (string directoryName in isf.GetDirectoryNames(subDirectory01))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += directoryName + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                txtMsg.Text += "目录 Directory01/SubDirectory01 下的*.txt文件列表:\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 获取 Directory01/SubDirectory01 目录下的后缀名为 txt 的文件名数组
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                foreach (string fileName in isf.GetFileNames(System.IO.Path.Combine(subDirectory01, "*.txt")))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += fileName + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (isf.FileExists(file01))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // 在文件 file01 中写入内容
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream streamWrite = isf.OpenFile(file01, FileMode.Open, FileAccess.Write);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        using (StreamWriter sw = new StreamWriter(streamWrite))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                                sw.WriteLine("我是:webabcd");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                                sw.WriteLine("我专注于asp.net, Silverlight");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += "文件 File01.txt 的内容:\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // 读取文件 file01 中的内容
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        IsolatedStorageFileStream streamRead = isf.OpenFile(file01, FileMode.Open, FileAccess.Read);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        using (StreamReader sr = new StreamReader(streamRead))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                                txtMsg.Text += sr.ReadToEnd();
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // 删除文件 file01
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                if (isf.FileExists(file01))
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.DeleteFile(file01);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                try
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // 删除目录 subDirectory01
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        isf.DeleteDirectory(subDirectory01);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                catch (IsolatedStorageException ex)
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // IsolatedStorageException - 操作临时存储失败时抛出的异常
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        // 因为 subDirectory01 目录内还有文件,所以会抛异常
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        txtMsg.Text += ex.Message;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// <summary>
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// 演示 IsolatedStorageSettings
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                /// </summary>
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                void Demo2()
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        //         IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        //         IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // Add(key, value) - 添加一对 key-value
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        iss.Add("key", "value");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        txtMsg2.Text += (string)iss["key"] + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // 修改指定的 key 的 value
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        iss["key"] = "value2";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        txtMsg2.Text += (string)iss["key"] + "\r\n";
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // Remove(key) - 移除指定的 key
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // Count - 字典表内的总的 key-value 数
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        iss.Remove("key");
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        txtMsg2.Text += iss.Count;
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                private void increase_Click(object sender, RoutedEventArgs e)
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        // 演示独立存储的配额的相关操作
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        {
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // Quota - 当前配额(KB)
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // IncreaseQuotaTo(newQuotaSize) - 增加到指定的配额
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                // AvailableFreeSpace - 当前的可用配额
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                isf.IncreaseQuotaTo(isf.Quota + 1 * 1024 * 1024);
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                System.Windows.Browser.HtmlPage.Window.Alert(
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                                        string.Format("当前配额:{0};可用配额:{1}", isf.Quota, isf.AvailableFreeSpace));
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                        }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated                }    
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated        }
稳扎稳打Silverlight(16) - 2.0数据之独立存储(Isolated Storage)_Isolated}
 
 
演示 IsolatedStorageFile 的运行结果:
根目录下的目录列表:
Directory01
Directory02
根目录下的文件列表:
RootFile01.txt
__LocalSettings
目录 Directory01 下的目录列表:
SubDirectory01
目录 Directory01/SubDirectory01 下的*.txt文件列表:
File01.txt
File02.txt
文件 File01.txt 的内容:
我是:webabcd
我专注于asp.net, Silverlight
无法删除,目录不为空或不存在。

演示 IsolatedStorageSettings 的运行结果:
value
value2
0


OK
[×××]