Silverlight独立存储
<UserControl x:Class="ISORW.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel Width="400" Height="400" Orientation="Vertical" Background="Blue" >
<StackPanel Width="400" Height="30" Orientation="Horizontal" Background="Blue">
<Button x:Name="btnWrite" Height="30" Width="150" Content="写入文件" Click="btnWrite_Click" Margin="20,0,0,0" ></Button>
<Button x:Name="btnRead" Height="30" Width="150" Content="读出文件" Click="btnRead_Click" Margin="50,0,0,0" ></Button>
</StackPanel>
<StackPanel Width="400" Height="20" Orientation="Horizontal" Background="Blue">
<TextBlock Height="20" Width="150" Text="写入文件" Margin="20,0,0,0" Foreground="Yellow" ></TextBlock>
<TextBlock Height="20" Width="150" Text="读取文件" Margin="50,0,0,0" Foreground="Yellow" ></TextBlock>
</StackPanel>
<StackPanel Width="400" Height="250" Orientation="Horizontal" Background="Blue">
<TextBox x:Name="tbTexts" Height="220" Width="180" Foreground="Black" TextWrapping="Wrap" Margin="5,0,10,10" Text="请在此输入需要写入文件的内容"></TextBox>
<TextBox x:Name="tbTextsRead" Height="220" Width="180" Foreground="Blue" TextWrapping="Wrap" Margin="5,0,10,10"></TextBox>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>
CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.IO.IsolatedStorage;
namespace ISORW
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
//执行写入命令
private void btnWrite_Click(object sender, RoutedEventArgs e)
{
//创建isolatedstoragefile,获取读写权限
IsolatedStorageFile isf =IsolatedStorageFile.GetUserStoreForApplication();
Stream stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write, isf);
System.IO.TextWriter writer = new System.IO.StreamWriter(stream);
writer.WriteLine(this.tbTexts.Text.ToString());
writer.Close(); // Close the writer so data is flushed
stream.Close(); // Close the stream too
}
//执行读取命令
private void btnRead_Click(object sender, RoutedEventArgs e)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
Stream stream = new IsolatedStorageFileStream("MyText.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, isf);
System.IO.TextReader reader = new System.IO.StreamReader(stream);
string sLine = reader.ReadLine();
this.tbTextsRead.Text = sLine;
reader.Close(); // Close the reader
stream.Close(); // Close the stream
}
}
}
浙公网安备 33010602011771号