分享

SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取

 Wiley Library 2011-09-10

SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取

概述

IsolatedStorageFile 成员

表示包含文件和目录的独立存储区。
IsolatedStorageFile.GetUserStoreForApplication

获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储。

IsolatedStorageFileStream 成员
公开独立存储中的文件。

StreamWriter 成员
实现一个 TextWriter,使其以一种特定的编码向流中写入字符。

注意

用户独立存储的空间默认为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");
}

}

}

大功告成!!!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多