简体   繁体   中英

How can I lock a file in .net so that only my app can access it?

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 ie 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.

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.

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".

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..

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM