一种选择是使用Isolated Storage。即使用户可以打开在独立存储下物理创建的文件,也很难找到该文件(请检查以下示例中的目录是如何创建的)您还可以为创建的存储设置适当的安全策略并其他应用程序无法访问同一文件。
对于桌面应用程序,隔离存储是一种数据存储机制,
通过定义标准化的方法来提供隔离和安全
将代码与保存的数据相关联。标准化提供了其他
好处也是如此。管理员可以使用旨在操纵的工具
隔离存储配置文件存储空间,设置安全性
策略,并删除未使用的数据。使用隔离存储,您的代码没有
更长的时间需要唯一的路径来指定文件中的安全位置
系统,并且数据不受其他应用程序的保护,这些应用程序只有
隔离存储访问。指示位置的硬编码信息
应用程序所在的存储区域是不必要的。
在独立存储下创建目录和文件
C#
using (IsolatedStorageFile storage = IsolatedStorageFile.GetStore((IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User), null, null))
{
storage.CreateDirectory(@"SampleStorageFolder");
storage.CreateFile(@"SampleStorageFolder\ReadMe.txt");
}
VB.NET
Imports System.IO.IsolatedStorage
Using storage As IsolatedStorageFile = IsolatedStorageFile.GetStore((IsolatedStorageScope.Domain Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.User), Nothing, Nothing)
storage.CreateDirectory("SampleStorageFolder")
storage.CreateFile("SampleStorageFolder\ReadMe.txt")
End Using
以上代码将在独立存储下创建目录和文件,
C:\Users\agarajah\AppData\Local\IsolatedStorage\f1fbq2pf.hsm\dkuvmluc.cgn\Url.q4hqkailhblwbougknr2gnmsmovpnjjc\Url.q4hqkailhblwbougknr2gnmsmovpnjjc\Files\SampleStorageFolder\ReadMe.txt
读取隔离存储下创建的文件
C#
using (IsolatedStorageFile storage = IsolatedStorageFile.GetStore((IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly | IsolatedStorageScope.User), null, null))
{
using (StreamReader reader = new StreamReader(storage.OpenFile("ReadMe.txt", FileMode.Open)))
{
string content = reader.ReadToEnd();
}
}
VB.NET
Using storage As IsolatedStorageFile = IsolatedStorageFile.GetStore((IsolatedStorageScope.Domain Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.User), Nothing, Nothing)
Using reader As New StreamReader(storage.OpenFile("ReadMe.txt", FileMode.Open))
Dim content As String = reader.ReadToEnd()
End Using
End Using
注意:尝试从另一个应用程序读取相同的文件会导致 FileNotFoundException 出现错误消息“找不到文件 'ReadMe.txt'。”。