(2)[wp7数据存储] WP7 IsolatedStorage系列篇——获取存储的文件或文件夹 [复制链接]

发表于 2012-5-17 15:46:43 |只看该作者 |倒序浏览

 

本帖最后由 agameboy 于 2012-5-17 16:59 编辑

之前写过添加文件和文件加的例子,这里就不再重复了,更多的文章请参考WP7 IsolatedStorage系列篇,下面是获取存储的所有文件和文件夹的代码片段,主要会用到:
IsolatedStorageFile
IsolatedStorageFileStream
StreamWriter
StreamReader

代码:
  1. using System.IO.IsolatedStorage;
  2. using System.IO;
  3. namespace IsolatedStorage
  4. {
  5.     public partial class MainPage : PhoneApplicationPage
  6.     {
  7.         IsolatedStorageFile _iso;
  8.         // Constructor
  9.         public MainPage()
  10.         {
  11.             InitializeComponent();
  12.             _iso = IsolatedStorageFile.GetUserStoreForApplication();
  13.            
  14.         }
  15.         private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
  16.         {
  17.             ApplicationTitle.Text = string.Format("共{0}个文件" + "{1}个文件夹",
  18. _iso.GetFileNames().Length, _iso.GetDirectoryNames().Length);
  19.             foreach (var filename in _iso.GetDirectoryNames ())
  20.             {
  21.                 ListBoxItem item = new ListBoxItem();
  22.                 item.Content ="[文件夹]"+ filename;
  23.                 item.Tag = filename;
  24.                 item.Height = 40;
  25.                 this.listBox1.Items.Add(item);
  26.             }
  27.             foreach (var filename in _iso.GetFileNames())
  28.             {
  29.                 ListBoxItem item = new ListBoxItem();
  30.                 item.Content = "[文件]" + filename;
  31.                 item.Tag = filename;
  32.                 item.Height = 40;
  33.                 listBox1.Items.Add(item );
  34.             }
  35.         }
  36.         private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
  37.         {
  38.             ListBoxItem item = (ListBoxItem)listBox1.Items[listBox1.SelectedIndex];
  39.             if (!_iso.DirectoryExists(item.Tag.ToString()))
  40.             {
  41.                 IsolatedStorageFileStream isoStream=new
  42. IsolatedStorageFileStream (item.Tag.ToString(),FileMode.Open
  43. ,FileAccess.Read , _iso );
  44.                 StreamReader streamReader = new StreamReader(isoStream );
  45.                 this.textBlock1.Text = streamReader.ReadToEnd();
  46.                 streamReader.Close();
  47.             }
  48.             else
  49.             {
  50.                 this.textBlock1.Text = "您单击了一个文件夹!";
  51.             }
  52.         }
  53.     }
  54. }
复制代码
posted @ 2012-11-29 14:32  BellingWP  阅读(130)  评论(0编辑  收藏  举报