Windows Phone7隔离存储空间
隔离存储空间:
• 目录操作
• 文件操作
• 应用程序配置信息
隔离存储空间的概念:所有文件io操作被限制在隔离存储空间里面,在隔离存储空间里面可以增删改目录和文件,在隔离存储空间里面可以存储程序配置信息
重要的类:
• isolatedstoragefile用于操控隔离存储空间里面的目录及文件,
• isolatedstoragefilestream用于读写操控隔离存储空间里面的流
• isolatedstoragefilesettings用于存储程序配置信息的dictionary
配额管理:
• windows phone下的隔离存储空间没有配额的限制
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 microsoft.phone.controls;
using system.io.isolatedstorage;
using system.io;
namespace isolatedstorage
{
public partial class mainpage : phoneapplicationpage
{
// constructor
public mainpage()
{
initializecomponent();
}
private const string foldername = "temp1";
private const string filename = foldername + "/address.txt";
private const string settingname = "sname";
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
file.createdirectory(foldername);
}
}
/// <summary>
/// 检查文件夹是否存在
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
if (file.directoryexists(foldername))
{
messagebox.show("已存在");
}
else
{
messagebox.show("不存在");
}
}
}
/// <summary>
/// 删除目录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
file.deletedirectory(foldername);
}
}
/// <summary>
/// 创建文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
isolatedstoragefilestream stream = file.createfile(filename);
stream.close();
}
}
/// <summary>
/// 检查文件是否存在
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
if (file.fileexists(filename))
{
messagebox.show("已存在" + filename);
}
else
{
messagebox.show("不存在");
}
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
file.deletefile(filename);
}
}
/// <summary>
/// 向文件里增加内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
using (isolatedstoragefilestream stream = file.openfile(filename, filemode.openorcreate, fileaccess.readwrite))
{
streamwriter writer = new streamwriter(stream);
writer.writeline(textbox1.text);
writer.close();
textbox1.text = "";
}
}
}
/// <summary>
/// 读取文件内容
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_click(object sender, routedeventargs e)
{
using (isolatedstoragefile file = isolatedstoragefile.getuserstoreforapplication())
{
using (isolatedstoragefilestream stream = file.openfile(filename, filemode.openorcreate, fileaccess.readwrite))
{
using (streamreader reader=new streamreader (stream))
{
textbox1.text = reader.readtoend();
}
}
}
}
/// <summary>
/// 程序配置信息保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button9_click(object sender, routedeventargs e)
{
isolatedstoragesettings.applicationsettings[settingname] = textbox2.text;
isolatedstoragesettings.applicationsettings.save();
textbox2.text = "";
}
/// <summary>
/// 程序配置信息读取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_click(object sender, routedeventargs e)
{
if (isolatedstoragesettings.applicationsettings.contains(settingname))
{
textbox2.text = isolatedstoragesettings.applicationsettings[settingname].tostring();
}
}
}
}
摘自 麻将我会
推荐阅读