如何锁定 .net 中的文件以便只有我的应用程序可以访问它?
我阅读了有关文件锁定的查询,但它们要么涉及多个文件访问问题,要么涉及其他问题。对此的通常响应是使用 FileStream.Lock 锁定文件,使用 FileShare.None 和 File.Open 等,但锁定说 100 个文件这不是一个好的编程实践,而且在这些情况下,如果我的应用程序关闭/崩溃,锁将被释放,这是我不希望的,即我希望锁持续存在,并且只有我的应用程序可以为其他人打开它。
在msdn上我找到了System.Security、System.Security.AccessControl命名空间,我相信这些可能会有所帮助。另外这篇文章将阐明我想要的东西。
编辑::
Mannnnn ...有没有人引用我提供的“此”链接。
好吧...总之我想要一些 PC 安全软件所做的事情。这听起来像吗...有人用过吗???
谢谢。
I read queries regarding file locking but either they refer to multiple file access problem or something else.Ususal response to this would be lock to the file using FileStream.Lock,using FileShare.None with File.Open etc, but locking say a 100 files is not a good programming practice,also in these scenarios the lock will be released if my app closes/crashes which I don't want also i.e. I want the lock to persist and only my app can open it for others.
On msdn I found System.Security,System.Security.AccessControl namespaces,I believe these might help.Also this article will shed some light on what I want.
EDIT::
Mannnnn... did anyone referred the "this" link I provided.
Ok...in short I want something what PC Security software does.Does that ring a bell...anyone used it????
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是不可能的。具有足够权限的用户始终可以更改安全设置并运行程序来访问您的文件。因此,请假设您无法阻止此操作,并专注于程序或用户可以对文件中的数据执行哪些操作。
加密它。
This just isn't possible. A user with sufficient privileges can always alter security settings and run a program to access your file. So, work from the assumption that you cannot stop this and focus on what the program or the user could do with the data in the file.
Encrypt it.
您的要求有一个基本缺陷,那就是文件系统只有一种方法来阻止访问文件,那就是通过 ACL。您可以指定登录系统的用户没有这些文件的访问权限,因此这些文件对他来说不可用。
但是,为了使您的程序能够访问这些文件,您需要设置一个“登录”用户才能运行您的程序,当然,配置为以该用户身份运行的任何其他程序都将有权访问那些文件。
正如您已经发现的那样,其他锁定(例如打开文件以进行独占访问)仅在程序运行时有效。如果由于某种原因程序没有运行,则这些类型的锁就不会到位。
您想要阻止什么具体?除了尝试锁定文件之外,可能还有其他方法可以解决这个问题。
There's a basic flaw in your requirements here and that is that the file system only has one method of preventing access to a file, and that is through the ACL. You can specify that the user that logs on to the system doesn't have access rights to those files, and thus the files are not available to him.
However, in order for your program to be able to access those files, you need to set up a user that "logs on" in order to run your program, and of course any other programs configured to run as that user will have access to those files.
Other locks, like open the file for exclusive access, is as you've already discovered only effective when the program is running. If for any reason the program isn't running, those types of locks aren't in place.
What specifically are you trying to prevent? There might be other ways to handle that than to try to lock away the files.
您可以加密该文件并将密钥嵌入到您的应用程序中。这取决于您所说的“只有我的应用程序可以为其他人打开它”是什么意思。
You could encrypt the file and embed the key within your application. It depends on what you mean by "only my app can open it for others".
我建议使用 IsolatedStorage 类。您可以在那里存储直接“链接”到您的应用程序的文件。
请在MSDNisolatedStorage。您可以使用文件程序集范围。无论如何,每个用户可以通过文件系统导航到applicationData文件夹并手动打开此文件(尽管这是相当意外的)
I would recommend using the IsolatedStorage class. There you can store a file which is directly "linked" to your application.
Have a closer look to it in MSDN IsolatedStorage. You may use a file assembly scoped. Anyway each user MAY navigate through the file system to your applicationData folder and open this file manually (although it is rather unexpected)
您正在尝试找到一种方法来实现方法(“锁定”文件)而不是实现目标。
我假设您想要保存一些私有数据,并可选择向其他应用程序提供对您的数据的访问权限。在这种情况下,您需要一个加密的容器,它可以暴露给其他应用程序,例如通过虚拟磁盘(我们的 SolFS在这种情况下,操作系统版本是最佳变体)。
然而,问题是密钥将保存在应用程序的代码中,理论上可以提取并用于访问容器。实际上,该过程可以变得更加复杂,但这并不是 100% 的解决方案。
You are trying to find a way to implement a method ("lock" the files) and not to achieve a goal.
I assume that you want to hold some private data and optionally provide access to your data to other applications. In this case you need an encrypted container, which can be exposed to other applications, for example via a virtual disk (our SolFS OS edition is an optimal variant in this case).
However, the problem is that the key will be held in your application's code, and theoretically can be extracted and used to access the container. Practically the procedure can be made more complicated, but this is not a 100% bulletproof solution.
我认为你想要的东西只能通过使用文件系统微过滤器驱动程序来实现。由于许多其他应用程序通过使用通用屏蔽之类的驱动程序提供此功能,您可以在其中将进程设置为受信任,因此只有受信任的进程才能访问该文件/文件夹,其他应用程序(例如文件夹保护)也对文件/文件夹提供此类权限。
I think the thing which you want can only possible by using file system minifilter driver. as many other application provide this functionality by using driver like universal shield in which you can make process as trusted so only trusted process can access that file/folder an other application like folder protect which also provide such type of permission on file/folder..