SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取概述 IsolatedStorageFile 成员 表示包含文件和目录的独立存储区。 获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储。 IsolatedStorageFileStream 成员 StreamWriter 成员 注意 用户独立存储的空间默认为1M,所以大文件可能没有空间,此时可以扩展用户独立存储。 SilverLight浏览器交互之:SilverLight用户独立存储空间的计算和扩展效果
xaml代码:
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel> <Button x:Name="Write" Width="150" Height="50" Margin="5" Content="创建文件" Click="Write_Click" /> <Button x:Name="Read" Width="150" Height="50" Margin="5" Content="读取文件" Click="Read_Click" /> <TextBlock x:Name="myTextBlock" Text="" TextWrapping="Wrap" Margin="5" /> </StackPanel> </Grid> cs代码: /// <summary>
/// 创建文件并写入内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Write_Click(object sender, RoutedEventArgs e) { //IsolatedStorageFile 成员 //IsolatedStorageFile.GetUserStoreForApplication 获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储。 using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { //IsolatedStorageFileStream 成员 //公开独立存储中的文件。 using (var stream = new IsolatedStorageFileStream("file.txt", FileMode.Create, isoFile)) { //StreamWriter 成员 //实现一个 TextWriter,使其以一种特定的编码向流中写入字符。 using (var writer = new StreamWriter(stream)) { writer.Write("创建时间为:" + DateTime.Now ); } stream.Close(); MessageBox.Show("文件创建成功!"); } } } /// <summary> /// 查找文件并读取文件内容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Read_Click(object sender, RoutedEventArgs e) { using (var isoFile = IsolatedStorageFile.GetUserStoreForApplication()) { using (var stream = new IsolatedStorageFileStream("file.txt", FileMode.Open, isoFile)) { using (var writer = new StreamReader(stream)) { myTextBlock.Text = writer.ReadToEnd()+"读取时间为:"+DateTime.Now; } stream.Close(); // isoFile.DeleteFile("file1.txt"); } } } 大功告成!!! |
|