Silverlight特色的独立存储区--IsolatedStorage

      IsolatedStorage是Silverlight一个特色,它是Silverlight的虚拟文件系统,所谓的虚拟文件系统,微软官方文档解释为虚拟文件系统的根位于物理文件系统上经过模糊处理的每用户文件夹中,由主机提供的每个唯一标识符都映射为不同的根,该根为每个应用程序提供它自己的虚拟文件系统。应用程序不能从它自己的文件系统导航到另一个文件系统中。也就是说除了一些高度受信任的托管代码和管理工具可以从其他程序集访问还有非托管代码可以访问外,此空间就只能本程序访问,由此它的安全性很高。Windows Phone 7是基于Silverlight,它的文件系统也是IsolatedStorage,它的默认大小为2GB,因为他的安全性,他不适合保存大的数据和重要数据,适合保存临时数据。

    IsolatedStorage命名空间是 System.IO.IsolatedStorage,它包含在 mscorlib程序集中,在命名空间下有四个类,它们分别是:IsolatedStorageFile、IsolatedStorageFileStream、IsolatedStorageExceptionIsolatedStorageSettings,其中IsolatedStorageFile、IsolatedStorageFileStream为常用,下面有一个简单示例运用这两个类。

    我们在IsolatedStorage中写入文件和和读取文件简单操作,首先新建一个项目,在MainPage.xaml写Xaml Code:

<phone:PhoneApplicationPage 
x:Class="Demo.MainPage"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone
="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell
="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable
="d" d:DesignWidth="480" d:DesignHeight="696"
FontFamily
="{StaticResource PhoneFontFamilyNormal}"
FontSize
="{StaticResource PhoneFontSizeNormal}"
Foreground
="{StaticResource PhoneForegroundBrush}"
SupportedOrientations
="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible
="True">

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="写入文件" Height="72" HorizontalAlignment="Left" Margin="9,148,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
<Button Content="读取文件" Height="72" HorizontalAlignment="Left" Margin="205,148,0,0" Name="button3" VerticalAlignment="Top" Width="160" Click="button3_Click" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="-4,48,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="460" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="6,226,0,0" Name="textBlock1" Text="" VerticalAlignment="Top" />
</Grid>
</Grid>

<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBar.MenuItems>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>
</shell:ApplicationBar.MenuItems>
</shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>
-->

</phone:PhoneApplicationPage>

  


      下来在MainPage类定义独立存储对象,它对应着特定的独立存储范围,在构造函数中初始化。

   

 IsolatedStorageFile isf;
public MainPage()
{
InitializeComponent();
//获取应用程序的空间,初始化
isf = IsolatedStorageFile.GetUserStoreForApplication();
}

        最后使用 IsolatedStorageFileStream 类,为写入文件和读取文件按钮添加事件。

        C# code:

 private void button2_Click(object sender, RoutedEventArgs e)
{
//写入文件操作
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("text.txt",FileMode.Create,FileAccess.Write,isf);
StreamWriter writer
= new StreamWriter(fileStream);
if (this.textBox1.Text != "")
{
writer.WriteLine(
this.textBox1.Text);
writer.Close();
}
else
{
MessageBox.Show(
"写入文件不能为空!");
}
}

private void button3_Click(object sender, RoutedEventArgs e)
{
//读取文件操作
IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream("text.txt", FileMode.Open, FileAccess.Read,isf);
StreamReader reader
= new StreamReader(fileStream);
this.textBlock1.Text = reader.ReadToEnd();
reader.Close();
}

  好了,简单的文件写读就完成,就到这里了。

  

posted @ 2011-08-02 18:06  flute  阅读(1703)  评论(8编辑  收藏  举报