Silverlight之独立存储(10)

独立存储

代码下载

1.独立存储

独立存储是一种数据存储机制,它在代码与保存的数据之间定义了标准化的关联方式,从而提供隔离性和安全性。同时,标准化也提供了其他好处。管理员可以使用旨在操作独立存储的工具来配置文件存储空间、设置安全策略及删除未使用的数据。通过独立存储,代码不再需要使用唯一的路径来指定文件系统中的安全位置,同时可以保护数据免遭只具有独立存储访问权限的其他应用程序的损坏。不再需要指示应用程序的存储区域位置的硬编码信息。

2.代码实现例子关键代码

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();

StringBuilder sb = new StringBuilder();

//创建文件夹

store.CreateDirectory("dir1");

stringsub11 = Path.Combine("dir1", "SubDir11");

stringsub12 = Path.Combine("dir1", "SubDir12");

stringsub13 = Path.Combine("dir1", "SubDir13");

store.CreateDirectory(sub11);

store.CreateDirectory(sub12);

store.CreateDirectory(sub13);

store.CreateDirectory("dir2");

store.CreateDirectory("dir2\\sub21");

store.CreateDirectory("dir3");

//创建文件

IsolatedStorageFileStreamf1 = store.CreateFile(Path.Combine(sub11,"InTheRoot1.txt"));

f1.Close();

IsolatedStorageFileStreamf2 = store.CreateFile("InTheRoot12.txt");

f2.Close();

IsolatedStorageFileStreamf3 = store.CreateFile("InTheRoot13.txt");

f3.Close();

//得到文件夹

string[]dirs = store.GetDirectoryNames();

string[]subdirs = store.GetDirectoryNames(Path.Combine("dir1","*"));

//得到文件

string[]files = store.GetFileNames();

stringsearchpath = Path.Combine(sub11, "*.*");

string[]filesInSubDirs = store.GetFileNames(searchpath);

//写文件

strings = "InTheRoot12.txt";

if(store.FileExists(s))

{

try

{

using(StreamWriter sw =newStreamWriter(store.OpenFile(s,FileMode.Open, FileAccess.Write)))

{

sw.WriteLine("滚滚长江东逝水!");

}

}

catch(IsolatedStorageException ex)

{

}

}

//读文件

try

{

using(StreamReader reader =new StreamReader(store.OpenFile(s,FileMode.Open,FileAccess.Read)))

{

stringcontents = reader.ReadToEnd();

sb.AppendLine(s + "内容:");

sb.AppendLine(contents);

}

}

catch(IsolatedStorageException ex)

{

sb.AppendLine(ex.Message);

}

//删除文件,删除文件夹

store.DeleteFile(Path.Combine(sub11,"InTheRoot1.txt"));

store.DeleteDirectory(sub11);

//store.Remove();

代码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值