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 ms开发者_开发问答dn 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 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.
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..
精彩评论