独立存储
1. 独立存储
独立存储是一种数据存储机制,它在代码与保存的数据之间定义了标准化的关联方式,从而提供隔离性和安全性。同时,标准化也提供了其他好处。管理员可以使用旨在操作独立存储的工具来配置文件存储空间、设置安全策略及删除未使用的数据。通过独立存储,代码不再需要使用唯一的路径来指定文件系统中的安全位置,同时可以保护数据免遭只具有独立存储访问权限的其他应用程序的损坏。不再需要指示应用程序的存储区域位置的硬编码信息。
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,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();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。