简体版  |  繁体版   推荐信息: 阅读排行 | 滚动 | 微软SOA高峰会 | 中型企业创新社区 | 随心所欲发新闻
存储

存储高手速成:Silverlight 2的独立存储

出处:论坛整理 作者:李会军 2008-07-03 14:09 评论
字体大小: | |
什么是独立存储?这个名字来自于哪里?独立存储(Isolated Storage)是Silverlight 2中提供的一个客户端安全的存储,它是一个与Cookie机制类似的局部信任机制。详情请参阅下文。

  独立存储(Isolated Storage)是Silverlight 2中提供的一个客户端安全的存储,它是一个与Cookie机制类似的局部信任机制。独立存储机制的APIs 提供了一个虚拟的文件系统和可以访问这个虚拟文件系统的数据流对象。Silverlight中的独立存储是基于 .NET Framework中的独立存储来建立的,所以它仅仅是.NET Framework中独立存储的一个子集。

  独立存储(Isolated Storage)是Silverlight 2中提供的一个客户端安全的存储,它是一个与Cookie机制类似的局部信任机制。独立存储机制的APIs 提供了一个虚拟的文件系统和可以访问这个虚拟文件系统的数据流对象。Silverlight中的独立存储是基于 .NET Framework中的独立存储来建立的,所以它仅仅是.NET Framework中独立存储的一个子集。

  Silverlight中的独立存储有以下一些特征:

  1.每个基于Silverlight的应用程序都被分配了属于它自己的一部分存储空间, 但是应用程序中的程序集却是在存储空间中共享的。一个应用程序被服务器赋给了一个唯一的固定的标识值。基于Silverlight的应用程序的虚拟文件系统现在就以一个标识值的方式来访问了。这个标识值必须是一个常量,这样每次应用程序运行时才可以找到这个共享的位置。

  2.独立存储的APIs 其实和其它的文件操作APIs类似,比如 File 和 Directory 这些用来访问和维护文件或文件夹的类。 它们都是基于FileStream APIs 来维护文件的内容的。

  3.独立存储严格的限制了应用程序可以存储的数据的大小,目前的上限是每个应用程序为1 MB。

  使用独立存储

  Silverlight中的独立存储功能通过密封类IsolatedStorageFile来提供,位于命名空间System.IO.IsolatedStorag中,IsolatedStorageFile类抽象了独立存储的虚拟文件系统。创建一个 IsolatedStorageFile 类的实例,可以使用它对文件或文件夹进行列举或管理。同样还可以使用该类的 IsolatedStorageFileStream 对象来管理文件内容,它的定义大概如下所示:

  

TerryLee_0072

  在Silverlight 2中支持两种方式的独立存储,即按应用程序存储或者按站点存储,可以分别使用GetUserStoreForApplication方法和GetUserStoreForSite方法来获取IsolatedStorageFile对象。下面看一个简单的示例,最终的效果如下图所示:

  

TerryLee_0073

  下面来看各个功能的实现:

  创建目录,直接使用CreateDirectory方法就可以了,另外还可以使用DirectoryExistes方法来判断目录是否已经存在:

  void btnCreateDirectory_Click(object sender, RoutedEventArgs e){ using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String directoryName = this.txtDirectoryName.Text; if (this.lstDirectories.SelectedItem != null) { directoryName = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), directoryName); } if (!store.DirectoryExists(directoryName)) { store.CreateDirectory(directoryName); HtmlPage.Window.Alert("创建目录成功!"); } }}

  创建文件,通过CreateFile方法来获取一个IsolatedStorageFileStream,并将内容写入到文件中:

  void btnCreateFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem == null && this.txtDirectoryName.Text == "") { HtmlPage.Window.Alert("请先选择一个目录或者输入目录名"); return; } using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath; if (this.lstDirectories.SelectedItem == null) { filePath = System.IO.Path.Combine(this.txtDirectoryName.Text, this.txtFileName.Text + ".txt"); } else { filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.txtFileName.Text + ".txt"); } IsolatedStorageFileStream fileStream = store.CreateFile(filePath); using (StreamWriter sw = new StreamWriter(fileStream)) { sw.WriteLine(this.txtFileContent.Text); } fileStream.Close(); HtmlPage.Window.Alert("写入文件成功!"); }}

  读取文件,直接使用System.IO命名空间下的StreamReader:

  void btnReadFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem == null || this.lstFiles.SelectedItem == null) { HtmlPage.Window.Alert("请先选择目录和文件!"); return; } using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.lstFiles.SelectedItem.ToString()); if (store.FileExists(filePath)) { StreamReader reader = new StreamReader(store.OpenFile(filePath, FileMode.Open, FileAccess.Read)); this.txtFileContent.Text = reader.ReadToEnd(); this.txtDirectoryName.Text = this.lstDirectories.SelectedItem.ToString(); this.txtFileName.Text = this.lstFiles.SelectedItem.ToString(); } }}

  删除目录和文件:

  void btnDeleteFile_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem != null && this.lstFiles.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String filePath = System.IO.Path.Combine(this.lstDirectories.SelectedItem.ToString(), this.lstFiles.SelectedItem.ToString()); store.DeleteFile(filePath); HtmlPage.Window.Alert("删除文件成功!"); } }}void btnDeleteDirectory_Click(object sender, RoutedEventArgs e){ if (this.lstDirectories.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { store.DeleteDirectory(this.lstDirectories.SelectedItem.ToString()); HtmlPage.Window.Alert("删除目录成功!"); } }}

  获取目录列表和文件列表:

  void lstDirectories_SelectionChanged(object sender, SelectionChangedEventArgs e){ if (lstDirectories.SelectedItem != null) { using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String[] files = store.GetFileNames( this.lstDirectories.SelectedItem.ToString() + "/"); this.lstFiles.ItemsSource = files; } }}void BindDirectories(){ using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) { String[] directories = store.GetDirectoryNames("*"); this.lstDirectories.ItemsSource = directories; }}

共3页。 1 2 3 :
  • 本文关键字:
  • 存储(3002)
  • 综述(3845)
  • 网友关注
    热门产品
    编辑推荐
    推荐专题
    更多
    思科
  • 打开网络创新之门
  • 思科公司于北京嘉里中心饭店成功举办了主题为“创新网络,绿色引擎”的思科创新日暨思科新品发布会。
  • 论坛热贴
    更多
    博客精选
    更多
    视频推荐
    更多
  • 杜青松:对IT人员要求别具一格
  • 在对杜青松的采访中,他透露出目前在中粮包装有限公司信息化建设的工作中的一个难点——IT人力资源短缺。
  • TMG

    Copyright (C) 1999-2008 Chinabyte.com, All Rights Reserved 版权所有 天极网络

    渝ICP证B2-20030003号 商务联系、网站内容、合作建议:010-82657868

    版权声明 在线提交意见反馈 Powered by 天极内容管理平台CMS4i

    经营性网站备案信息 网警备案 中国网站排名