Windows Phone 7 Belling‘s课堂(六) 独立存储空间(2)
隔离存储的文件读写与我们过去在其它.NET开发中的文件读写是没有区别的,只是在WP上我们用IsolatedStorageFileStream,
而不是传统的FileStream罢了,说白了,就是换了一个类名。。。
功能:
新建一个项目,在主页面上放一个文本框,用来输入要写入文件的内容,放两个按钮,一个用于写操作,一个用于读操作,再放一个TextBlock,用于显示从文件读入的内容。
前台UI代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <phone:PhoneApplicationPage 2 x:Class="PhoneApp1.MainPage" 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 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 10 FontFamily="{StaticResource PhoneFontFamilyNormal}" 11 FontSize="{StaticResource PhoneFontSizeNormal}" 12 Foreground="{StaticResource PhoneForegroundBrush}" 13 SupportedOrientations="Portrait" Orientation="Portrait" 14 shell:SystemTray.IsVisible="True"> 15 <StackPanel> 16 <StackPanel Margin="0,25" Orientation="Vertical"> 17 <TextBox Name="txtContent" HorizontalAlignment="Stretch" Height="185" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap"/> 18 <Button Name="btnWrite" HorizontalAlignment="Stretch" Height="auto" Content="将内容写入到文件" Click="btnWrite_Click"/> 19 </StackPanel> 20 <StackPanel Margin="0,25" Orientation="Vertical"> 21 <Button HorizontalAlignment="Stretch" Content="从文件中读入" Name="btnRead" Click="btnRead_Click"/> 22 <TextBlock Name="txtDisplay" HorizontalAlignment="Stretch" Height="358" ScrollViewer.VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" FontSize="35"/> 23 </StackPanel> 24 </StackPanel> 25 26 </phone:PhoneApplicationPage>
不太鼓励各位照搬的,即使按着抄一遍也有收获。。
后台CS代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Documents; 8 using System.Windows.Input; 9 using System.Windows.Media; 10 using System.Windows.Media.Animation; 11 using System.Windows.Shapes; 12 using Microsoft.Phone.Controls; 13 // 引入此命名空间 14 using System.IO; 15 using System.IO.IsolatedStorage; 16 17 namespace PhoneApp1 18 { 19 public partial class MainPage : PhoneApplicationPage 20 { 21 // 常量 22 const string MyDir = "MyData"; 23 const string testFileName = "file"; 24 25 // 构造函数 26 public MainPage() 27 { 28 InitializeComponent(); 29 this.Loaded += (sender, e) => 30 { 31 using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 32 //通过GetUserStoreForApplication静态方法,可以返回一个IsolatedStorageFile实例。 33 { 34 if (iso.DirectoryExists(MyDir) == false)//查看是否存在MyDir 35 { 36 iso.CreateDirectory(MyDir); //如果没有则创建目录 37 } 38 } 39 }; 40 } 41 42 43 private void btnWrite_Click(object sender, RoutedEventArgs e) 44 { 45 using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 46 //通过GetUserStoreForApplication静态方法,可以返回一个IsolatedStorageFile实例。 47 { 48 using (var sr = iso.CreateFile(MyDir + "\\" + testFileName)) 49 { 50 StreamWriter sw = new StreamWriter(sr); 51 sw.Write(txtContent.Text); 52 53 sw.Close(); 54 sw.Dispose(); 55 } 56 } 57 } 58 59 private void btnRead_Click(object sender, RoutedEventArgs e) 60 { 61 using (var iso = IsolatedStorageFile.GetUserStoreForApplication()) 62 //通过GetUserStoreForApplication静态方法,可以返回一个IsolatedStorageFile实例。 63 { 64 var sr = iso.OpenFile(MyDir + "\\" + testFileName, FileMode.Open, FileAccess.Read); 65 StreamReader reader = new StreamReader(sr); 66 string info = reader.ReadToEnd(); 67 reader.Close(); 68 reader.Dispose(); 69 sr.Close(); 70 sr.Dispose(); 71 txtDisplay.Text = info; 72 } 73 } 74 } 75 }
图片: