数据之独立存储(Isolated Storage)[转]

作者: webabcd

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.IO.IsolatedStorage;
using System.IO;

namespace Silverlight20.Data
{
     public  partial  class IsolatedStorage : UserControl
    {
         public IsolatedStorage()
        {
            InitializeComponent();

             //  演示 IsolatedStorageFile
            Demo();

             //  演示 IsolatedStorageSettings
            Demo2();
        }

         /**/ ///   <summary>
        
///  演示 IsolatedStorageFile
        
///   </summary>
         void Demo()
        {
             //  Isolated Storage - 独立存储。一个虚拟文件系统

            
//  IsolatedStorageFile - 操作 独立存储 的类
            
//      IsolatedStorageFile.GetUserStoreForSite() - 按站点获取用户的独立存储
            
//      IsolatedStorageFile.GetUserStoreForApplication() - 按应用程序获取用户的独立存储
            
            
//  using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                 //  DirectoryExists(path) - 指定的路径是否存在
                
//  CreateDirectory(path) - 创建指定的路径
                
//  FileExists(path) - 指定的文件是否存在
                
//  CreateFile(path) - 创建指定的文件
                
//  GetDirectoryNames() - 获取根目录下的目录名数组
                
//  GetFileNames()() - 获取根目录下的文件名数组
                
//  GetDirectoryNames(path) - 获取指定目录下的目录名数组
                
//  GetFileNames(path) - 获取指定目录下的文件名数组
                
//  OpenFile() - 打开指定的文件。具体参数参看文档
                
//  DeleteFile(path) - 删除指定的文件
                
//  DeleteDirectory(path) - 删除指定的目录(要求目录存在,且目录内无内容)
                
//  Remove() - 关闭 IsolatedStorageFile 对象并移除独立存储内的全部内容


                
//  在根目录下创建指定的目录
                 if (!isf.DirectoryExists( " Directory01 "))
                    isf.CreateDirectory( " Directory01 ");
                 if (!isf.DirectoryExists( " Directory02 "))
                    isf.CreateDirectory( " Directory02 ");

                 //  创建指定的子目录
                 string subDirectory01 = System.IO.Path.Combine( " Directory01 "" SubDirectory01 ");
                 string subDirectory02 = System.IO.Path.Combine( " Directory01 "" SubDirectory02 ");
                 if (!isf.DirectoryExists(subDirectory01))
                    isf.CreateDirectory(subDirectory01);
                 if (!isf.DirectoryExists(subDirectory02))
                    isf.CreateDirectory(subDirectory02);
                

                 //  根目录下创建指定的文件
                 if (!isf.FileExists( " RootFile.txt "))
                {
                    IsolatedStorageFileStream isfs = isf.CreateFile( " RootFile01.txt ");
                    isfs.Close();
                }

                 //  在指定的目录下创建指定的文件
                 string file01 = System.IO.Path.Combine(subDirectory01,  " File01.txt ");
                 string file02 = System.IO.Path.Combine(subDirectory01,  " File02.txt ");
                 string file03 = System.IO.Path.Combine(subDirectory01,  " File03.xml ");
                 if (!isf.FileExists(file01))
                {
                     //  IsolatedStorageFileStream - 独立存储内的文件流。继承自 FileStream
                    IsolatedStorageFileStream isfs = isf.CreateFile(file01);
                    isfs.Close();
                }
                 if (!isf.FileExists(file02))
                {
                    IsolatedStorageFileStream isfs = isf.CreateFile(file02);
                    isfs.Close();
                }
                 if (!isf.FileExists(file03))
                {
                    IsolatedStorageFileStream isfs = isf.CreateFile(file03);
                    isfs.Close();
                }


                txtMsg.Text +=  " 根目录下的目录列表:\r\n ";
                 //  获取根目录下的目录名数组
                 foreach ( string directoryName  in isf.GetDirectoryNames())
                {
                    txtMsg.Text += directoryName +  " \r\n ";
                }

                txtMsg.Text +=  " 根目录下的文件列表:\r\n ";
                 //  获取根目录下的文件名数组
                 foreach ( string fileName  in isf.GetFileNames())
                {
                    txtMsg.Text += fileName +  " \r\n ";
                }

                txtMsg.Text +=  " 目录 Directory01 下的目录列表:\r\n ";
                 //  获取 Directory01 目录下的目录名数组
                 foreach ( string directoryName  in isf.GetDirectoryNames(subDirectory01))
                {
                    txtMsg.Text += directoryName +  " \r\n ";
                }

                txtMsg.Text +=  " 目录 Directory01/SubDirectory01 下的*.txt文件列表:\r\n ";
                 //  获取 Directory01/SubDirectory01 目录下的后缀名为 txt 的文件名数组
                 foreach ( string fileName  in isf.GetFileNames(System.IO.Path.Combine(subDirectory01,  " *.txt ")))
                {
                    txtMsg.Text += fileName +  " \r\n ";
                }


                 if (isf.FileExists(file01))
                {
                     //  在文件 file01 中写入内容
                    IsolatedStorageFileStream streamWrite = isf.OpenFile(file01, FileMode.Open, FileAccess.Write);
                     using (StreamWriter sw =  new StreamWriter(streamWrite))
                    {
                        sw.WriteLine( " 我是:webabcd ");
                        sw.WriteLine( " 我专注于asp.net, Silverlight ");
                    }

                    txtMsg.Text +=  " 文件 File01.txt 的内容:\r\n ";
                     //  读取文件 file01 中的内容
                    IsolatedStorageFileStream streamRead = isf.OpenFile(file01, FileMode.Open, FileAccess.Read);
                     using (StreamReader sr =  new StreamReader(streamRead))
                    {
                        txtMsg.Text += sr.ReadToEnd();
                    }
                }


                 //  删除文件 file01
                 if (isf.FileExists(file01))
                {
                    isf.DeleteFile(file01);
                }

                 try
                {
                     //  删除目录 subDirectory01
                    isf.DeleteDirectory(subDirectory01);
                }
                 catch (IsolatedStorageException ex)
                {
                     //  IsolatedStorageException - 操作临时存储失败时抛出的异常

                    
//  因为 subDirectory01 目录内还有文件,所以会抛异常
                    txtMsg.Text += ex.Message;
                }
            }
        }

         /**/ ///   <summary>
        
///  演示 IsolatedStorageSettings
        
///   </summary>
         void Demo2()
        {
             //  IsolatedStorageSettings - 在独立存储中保存的 key-value 字典表
            
//      IsolatedStorageSettings.SiteSettings - 按站点保存的 key-value 字典表
            
//      IsolatedStorageSettings.ApplicationSettings - 按应用程序保存的 key-value 字典表

            IsolatedStorageSettings iss = IsolatedStorageSettings.ApplicationSettings;

             //  Add(key, value) - 添加一对 key-value
            iss.Add( " key "" value ");
            txtMsg2.Text += ( string)iss[ " key "] +  " \r\n ";

             //  修改指定的 key 的 value
            iss[ " key "] =  " value2 ";
            txtMsg2.Text += ( string)iss[ " key "] +  " \r\n ";

             //  Remove(key) - 移除指定的 key
            
//  Count - 字典表内的总的 key-value 数
            iss.Remove( " key ");
            txtMsg2.Text += iss.Count;
        }
       
         private  void increase_Click( object sender, RoutedEventArgs e)
        {
             //  演示独立存储的配额的相关操作
             using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
            {
                 //  Quota - 当前配额(KB)
                
//  IncreaseQuotaTo(newQuotaSize) - 增加到指定的配额
                
//  AvailableFreeSpace - 当前的可用配额

                isf.IncreaseQuotaTo(isf.Quota +  1 *  1024 *  1024);

                System.Windows.Browser.HtmlPage.Window.Alert(
                     string.Format( " 当前配额:{0};可用配额:{1} ", isf.Quota, isf.AvailableFreeSpace));
            }
        } 
    }
}

演示 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




转载于:https://www.cnblogs.com/arongbest/archive/2011/10/09/2203586.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值