silverlight独立存储示例
<UserControl x:Class="IsolatedStorageSample.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">
<TextBox x:Name="txtUser" Height="28" Margin="97,82,89,0" VerticalAlignment="Top" Text="Tom" TextWrapping="Wrap"/>
<Button x:Name="btnSave" Height="35" Margin="146,0,157,30" VerticalAlignment="Bottom" Content="确定"/>
<PasswordBox x:Name="txtPWD" Margin="97,127,89,110" Password="1848"/>
<TextBlock Height="23" Margin="135,55,113,0" VerticalAlignment="Top" Text="用户登录示例" TextWrapping="Wrap" FontSize="14" FontFamily="Arial"/>
<TextBlock Height="23" Margin="55,87,0,0" VerticalAlignment="Top" FontFamily="Arial" FontSize="14" Text="帐号" TextWrapping="Wrap" Width="38" HorizontalAlignment="Left"/>
<TextBlock Margin="55,129,0,113" FontFamily="Arial" FontSize="14" Text="口令" TextWrapping="Wrap" HorizontalAlignment="Left" Width="38" RenderTransformOrigin="0.553,0.696"/>
<TextBlock Height="19" Margin="97,0,89,78" VerticalAlignment="Bottom" Text="成功保存到isolatedStorage中" TextWrapping="Wrap" Foreground="#FF8D0000" FontSize="12" x:Name="lblInfo" Visibility="Collapsed"/>
</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 IsolatedStorageSample
{
public partial class Page : UserControl
{
string DirName = "Users";
string FileName = "Users/Default.txt";
public Page()
{
InitializeComponent();
ReadLocalInfo();
//按下Save按钮,执行保存操作
btnSave.Click += (o, e) =>
{
//帐号或口令为空都是不合法的
if (txtUser.Text == "" || txtPWD.Password == "")
return;
SaveLocalInfo();
};
}
//读取存储在IsolatedStorage中的信息
void ReadLocalInfo()
{
if (IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(DirName) == false)
{
return;
}
IsolatedStorageFileStream cfs = null;
//从本地文件中读取登录信息
cfs = new IsolatedStorageFileStream(FileName,
FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication());
StreamReader reader = new StreamReader(cfs);
string strUser = reader.ReadLine();
string strPWD = reader.ReadLine();
reader.Close();
cfs.Close();
//初始化控件
if (strUser != null) txtUser.Text = strUser;
if (strPWD != null) txtPWD.Password = strPWD;
}
//保存当前用户信息到IsolateStorage中Users目录下的Default.txt
void SaveLocalInfo()
{
IsolatedStorageFileStream cfs = null;
////判断该目录是否已经存在,不存在就新建一个
if (IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(DirName) == false)
{
IsolatedStorageFile.GetUserStoreForApplication().CreateDirectory(DirName);
}
//判断文件是否存在,不存在就新建一个
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(FileName) == false)
{
cfs = IsolatedStorageFile.GetUserStoreForApplication().CreateFile(FileName);
cfs.Close();
}
//从本地文件中读取XML信息
cfs = new IsolatedStorageFileStream(FileName,
FileMode.Truncate,
IsolatedStorageFile.GetUserStoreForApplication());
StreamWriter writer = new StreamWriter(cfs);
writer.WriteLine(txtUser.Text);
writer.WriteLine(txtPWD.Password);
writer.Close();
cfs.Close();
lblInfo.Visibility = Visibility.Visible;
}
}
}
浙公网安备 33010602011771号