是否有办法让文件夹和/或文件只能由应用程序访问
我记得玩游戏的地方有一个包含资源的大文件,只有应用程序才能访问它。
我正在尝试做同样的事情。
我已经考虑过压缩文件夹,但我不想将内容提取到某个位置,除非该位置只能由应用程序访问。
我也研究过加密,有没有办法保持整个文件夹或文件加密,让应用程序只能读取它?
我希望应用程序只能访问某些文件。
答案 0 :(得分:1)
一种选择是使用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
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'。”。
答案 1 :(得分:0)
您可以做的第一件事是创建一个应用程序浏览的隐藏文件夹。
加密: 加密文件对于没有经验的人来说似乎有风险。 你可以稍微加密扩展。 例如file.docx - > file.abc
这样你就不应该自己打开它, 然后你可以让应用程序知道哪个'假扩展'与哪个真实扩展相比较。
我希望这对你有所帮助!