WP7的IsolatedStorage类初步认识..

WP7的文件系统感觉很特殊,至少在7.0版本是这样的,不知道以后会不会有改进。

系统会分配应用一个独立的空间,该应用无法访问除自己空间外的空间。

想做一个文件管理系统也没办法...

主要用到:

IsolatedStorageFile
IsolatedStorageFileStream
StreamWriter
StreamReader

WP7的IsolatedStorage类初步认识..  WP7的IsolatedStorage类初步认识..  WP7的IsolatedStorage类初步认识..


Mainpage.xaml代码:

View Code
 1   
2 <phone:PhoneApplicationPage
3 x:Class="WindowsPhoneApplication1.MainPage"
4 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
7 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
8 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
9 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
11 FontFamily="{StaticResource PhoneFontFamilyNormal}"
12 FontSize="{StaticResource PhoneFontSizeNormal}"
13 Foreground="{StaticResource PhoneForegroundBrush}"
14 SupportedOrientations="Portrait" Orientation="Portrait"
15 shell:SystemTray.IsVisible="True">
16
17 <!--LayoutRoot is the root grid where all page content is placed-->
18 <Grid x:Name="LayoutRoot" Background="Transparent">
19 <Grid.RowDefinitions>
20 <RowDefinition Height="Auto"/>
21 <RowDefinition Height="*"/>
22 </Grid.RowDefinitions>
23
24 <!--TitlePanel contains the name of the application and page title-->
25 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
26 <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
27 <TextBlock x:Name="PageTitle" Text="IsolatedStorage" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
28 </StackPanel>
29
30 <!--ContentPanel - place additional content here-->
31 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>
32 </Grid>
33
34 <!--Sample code showing usage of ApplicationBar-->
35 <phone:PhoneApplicationPage.ApplicationBar>
36 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
37 <shell:ApplicationBarIconButton IconUri="/icon/appbar.next.rest.png" Text="查看文件" x:Name="GoFileListPageButton" Click="GoFileListPageButton_Click"/>
38 <shell:ApplicationBar.MenuItems>
39 <shell:ApplicationBarMenuItem Text="新建一个文件" Click="AddTextButton_Click"/>
40 <shell:ApplicationBarMenuItem Text="新建一个文件夹" Click="ApplicationBarMenuItem_Click"/>
41 </shell:ApplicationBar.MenuItems>
42 </shell:ApplicationBar>
43 </phone:PhoneApplicationPage.ApplicationBar>
44
45 </phone:PhoneApplicationPage>

Mainpage.xaml.cs代码:

View Code
 1   
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Net;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Animation;
12 using System.Windows.Shapes;
13 using Microsoft.Phone.Controls;
14 using System.IO;
15 using System.IO.IsolatedStorage;
16 namespace WindowsPhoneApplication1
17 {
18 public partial class MainPage : PhoneApplicationPage
19 {
20 IsolatedStorageFile _Iso;
21 // Constructor
22 public MainPage()
23 {
24 InitializeComponent();
25 _Iso = IsolatedStorageFile.GetUserStoreForApplication();
26 }
27
28
29 private void AddTextButton_Click(object sender, EventArgs e)
30 {
31 string DateStr = DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "[" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + "]";
32 IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(string
33 .Format("{0}.txt", DateStr), FileMode.Create, FileAccess.Write, _Iso);
34 StreamWriter Writer = new StreamWriter(isoStream);
35 Writer.WriteLine(DateStr);
36 Writer.Close();
37 }
38
39 private void GoFileListPageButton_Click(object sender, EventArgs e)
40 {
41 this.NavigationService.Navigate(new Uri("/FileList.xaml", UriKind.Relative));
42 }
43
44 private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
45 {
46 this.NavigationService.Navigate(new Uri("/Addfolder.xaml", UriKind.Relative));
47 }
48 }
49 }




AddFolder.Xaml

View Code
 1 <phone:PhoneApplicationPage 
2 x:Class="WindowsPhoneApplication1.Addfolder"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 FontFamily="{StaticResource PhoneFontFamilyNormal}"
10 FontSize="{StaticResource PhoneFontSizeNormal}"
11 Foreground="{StaticResource PhoneForegroundBrush}"
12 SupportedOrientations="Portrait" Orientation="Portrait"
13 mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
14 shell:SystemTray.IsVisible="True">
15
16 <!--LayoutRoot is the root grid where all page content is placed-->
17 <Grid x:Name="LayoutRoot" Background="Transparent">
18 <Grid.RowDefinitions>
19 <RowDefinition Height="Auto"/>
20 <RowDefinition Height="*"/>
21 </Grid.RowDefinitions>
22
23 <!--TitlePanel contains the name of the application and page title-->
24 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
25 <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
26 <TextBlock x:Name="PageTitle" Text="新建文件夹" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
27 </StackPanel>
28 <Button Content="保存" Grid.Row="1" Height="72" Margin="308,322,12,0" Name="button_Save" VerticalAlignment="Top" Click="button_Save_Click" />
29 <TextBox Grid.Row="1" Height="72" Margin="8,244,12,0" Name="textBox_Str" Text="" VerticalAlignment="Top" />
30 <!--ContentPanel - place additional content here-->
31 </Grid>
32
33 <!--Sample code showing usage of ApplicationBar-->
34 <!--<phone:PhoneApplicationPage.ApplicationBar>
35 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
36 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
37 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
38 <shell:ApplicationBar.MenuItems>
39 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
40 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
41 </shell:ApplicationBar.MenuItems>
42 </shell:ApplicationBar>
43 </phone:PhoneApplicationPage.ApplicationBar>-->
44
45 </phone:PhoneApplicationPage>
46
47
48


AddFolder.Xaml.cs

View Code
 1   
2
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Net;
7 using System.Windows;
8 using System.Windows.Controls;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Animation;
13 using System.Windows.Shapes;
14 using Microsoft.Phone.Controls;
15 using System.IO;
16 using System.IO.IsolatedStorage;
17 namespace WindowsPhoneApplication1
18 {
19 public partial class Addfolder : PhoneApplicationPage
20 {
21 IsolatedStorageFile _ISO;
22 public Addfolder()
23 {
24 InitializeComponent();
25 _ISO = IsolatedStorageFile.GetUserStoreForApplication();
26 }
27
28 private void button_Save_Click(object sender, RoutedEventArgs e)
29 {
30 if (this.textBox_Str.Text.Trim().Equals(""))
31 {
32 MessageBox.Show("未输入名称", "错误提示", MessageBoxButton.OK);
33 }
34 else {
35 _ISO.CreateDirectory(this.textBox_Str.Text.Trim());
36 this.textBox_Str.Text = "";
37 MessageBox.Show("建立文件夹成功", "提示", MessageBoxButton.OK);
38 }
39 }
40 }
41 }


FileList.Xaml

View Code
 1  
2 <phone:PhoneApplicationPage
3 x:Class="WindowsPhoneApplication1.FileList"
4 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
7 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
8 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
9 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
10 FontFamily="{StaticResource PhoneFontFamilyNormal}"
11 FontSize="{StaticResource PhoneFontSizeNormal}"
12 Foreground="{StaticResource PhoneForegroundBrush}"
13 SupportedOrientations="Portrait" Orientation="Portrait"
14 mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
15 shell:SystemTray.IsVisible="True"
16 Loaded="PhoneApplicationPage_Loaded"
17 >
18
19 <!--LayoutRoot is the root grid where all page content is placed-->
20 <Grid x:Name="LayoutRoot" Background="Transparent">
21 <Grid.RowDefinitions>
22 <RowDefinition Height="Auto"/>
23 <RowDefinition Height="*"/>
24 </Grid.RowDefinitions>
25
26 <!--TitlePanel contains the name of the application and page title-->
27 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
28 <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
29 <TextBlock x:Name="PageTitle" Text="文件查看" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
30 </StackPanel>
31
32 <!--ContentPanel - place additional content here-->
33 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
34 <ListBox Margin="0,6,3,283" Name="listBox_FileList" SelectionChanged="listBox_FileList_SelectionChanged" />
35 <TextBlock HorizontalAlignment="Left" Margin="0,341,0,6" Name="textBlock_Str" Text="" Width="450" />
36 </Grid>
37 </Grid>
38
39 <!--Sample code showing usage of ApplicationBar-->
40 <!--<phone:PhoneApplicationPage.ApplicationBar>
41 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
42 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
43 <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
44 <shell:ApplicationBar.MenuItems>
45 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
46 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
47 </shell:ApplicationBar.MenuItems>
48 </shell:ApplicationBar>
49 </phone:PhoneApplicationPage.ApplicationBar>-->
50
51 </phone:PhoneApplicationPage>
52
53
54

FileList.Xaml.cs

View Code
 1  
2 using System;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Net;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Animation;
12 using System.Windows.Shapes;
13 using Microsoft.Phone.Controls;
14 using System.IO;
15 using System.IO.IsolatedStorage;
16 namespace WindowsPhoneApplication1
17 {
18 public partial class FileList : PhoneApplicationPage
19 {
20 IsolatedStorageFile _ISO;
21 public FileList()
22 {
23 InitializeComponent();
24 _ISO = IsolatedStorageFile.GetUserStoreForApplication();
25 }
26
27 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
28 {
29
30 this.PageTitle.Text = string
31 .Format("共[{0}]个对象", _ISO.GetFileNames().Length + _ISO.GetDirectoryNames().Length);
32
33 foreach (string FileName in _ISO.GetDirectoryNames())
34 {
35 ListBoxItem Item = new ListBoxItem();
36 Item.Content = "[文件夹]" + FileName;
37 Item.Tag = FileName;
38 Item.Height = 40;
39
40 this.listBox_FileList.Items.Add(Item);
41 }
42 foreach (string FileName in _ISO.GetFileNames())
43 {
44 ListBoxItem Item = new ListBoxItem();
45 Item.Content = "[文件]" + FileName;
46 Item.Tag = FileName;
47 Item.Height = 40;
48
49 this.listBox_FileList.Items.Add(Item);
50 }
51 }
52
53 private void listBox_FileList_SelectionChanged(object sender, SelectionChangedEventArgs e)
54 {
55 ListBoxItem Item = (ListBoxItem)this.listBox_FileList.Items[this.listBox_FileList.SelectedIndex];
56 if (!_ISO.DirectoryExists(Item.Tag.ToString()))
57 {
58 IsolatedStorageFileStream Stream = new IsolatedStorageFileStream(Item.Tag.ToString(), FileMode.Open, FileAccess.Read, _ISO);
59
60 StreamReader Reader = new StreamReader(Stream);
61
62 this.textBlock_Str.Text = Reader.ReadToEnd();
63 Reader.Close();
64 }
65 else {
66 this.textBlock_Str.Text = "点击了一个文件夹";
67 }
68
69
70 }
71 }
72 }
73




你可能感兴趣的:(ora)