本帖最后由 agameboy 于 2012-5-17 16:59 编辑
之前写过添加文件和文件加的例子,这里就不再重复了,更多的文章请参考WP7 IsolatedStorage系列篇,下面是获取存储的所有文件和文件夹的代码片段,主要会用到: IsolatedStorageFile IsolatedStorageFileStream StreamWriter StreamReader 代码:
- using System.IO.IsolatedStorage;
- using System.IO;
- namespace IsolatedStorage
- {
- public partial class MainPage : PhoneApplicationPage
- {
- IsolatedStorageFile _iso;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- _iso = IsolatedStorageFile.GetUserStoreForApplication();
-
- }
- private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
- {
- ApplicationTitle.Text = string.Format("共{0}个文件" + "{1}个文件夹",
- _iso.GetFileNames().Length, _iso.GetDirectoryNames().Length);
- foreach (var filename in _iso.GetDirectoryNames ())
- {
- ListBoxItem item = new ListBoxItem();
- item.Content ="[文件夹]"+ filename;
- item.Tag = filename;
- item.Height = 40;
- this.listBox1.Items.Add(item);
- }
- foreach (var filename in _iso.GetFileNames())
- {
- ListBoxItem item = new ListBoxItem();
- item.Content = "[文件]" + filename;
- item.Tag = filename;
- item.Height = 40;
- listBox1.Items.Add(item );
- }
- }
- private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ListBoxItem item = (ListBoxItem)listBox1.Items[listBox1.SelectedIndex];
- if (!_iso.DirectoryExists(item.Tag.ToString()))
- {
- IsolatedStorageFileStream isoStream=new
- IsolatedStorageFileStream (item.Tag.ToString(),FileMode.Open
- ,FileAccess.Read , _iso );
- StreamReader streamReader = new StreamReader(isoStream );
- this.textBlock1.Text = streamReader.ReadToEnd();
- streamReader.Close();
- }
- else
- {
- this.textBlock1.Text = "您单击了一个文件夹!";
- }
- }
- }
- }
复制代码
|