Prevent access to directory
-
Hello, I am creating a directory programatically (C#) to store some application specific data. I want that this directory should't be deleted or modified while the application is running but once the application is closed, it can be modified or deleted. Also, all user on the machine should have similar rights for that folder. How do I achieve this? Any help in this direction will be greatly appreciated. Thanks.
-
Hello, I am creating a directory programatically (C#) to store some application specific data. I want that this directory should't be deleted or modified while the application is running but once the application is closed, it can be modified or deleted. Also, all user on the machine should have similar rights for that folder. How do I achieve this? Any help in this direction will be greatly appreciated. Thanks.
I think this can be achieved with the
FileSystemAccessRule
class (the creation of the rule(s) that is). Then you create aDirectorySecurity
ojbect and use theAddAccessRule
method to apply your access rule(s). Finally, create aDirectoryInfo
object representing the folder and run theSetAccessControl
method which takes yourDirectorySecurity
object as an argument. That's pretty much how I've done it in the past. Good luck! :) -
Hello, I am creating a directory programatically (C#) to store some application specific data. I want that this directory should't be deleted or modified while the application is running but once the application is closed, it can be modified or deleted. Also, all user on the machine should have similar rights for that folder. How do I achieve this? Any help in this direction will be greatly appreciated. Thanks.
Thanks for reply. I am using security like this:
AccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.Write | FileSystemRights.ReadAndExecute , InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Deny);
Still I am able to delete the folder manually. Thanks.
-
Thanks for reply. I am using security like this:
AccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.Write | FileSystemRights.ReadAndExecute , InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Deny);
Still I am able to delete the folder manually. Thanks.
You could open a file (located in the folder) with exclusive read/write, and any attempt to delete the folder will fail as long as that file is opened.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
You could open a file (located in the folder) with exclusive read/write, and any attempt to delete the folder will fail as long as that file is opened.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001Yeah.. That's good idea. The only concern is that folder can't be deleted but other containts of the folder can be deleted one by one. Anyway, this solution might resolve my problem. Thanks.
-
Thanks for reply. I am using security like this:
AccessRule rule = new FileSystemAccessRule("Users", FileSystemRights.Write | FileSystemRights.ReadAndExecute , InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Deny);
Still I am able to delete the folder manually. Thanks.
-
That's odd.. Do you mean that after you apply a rule that deny
Users
to do something, a member of the very same group is still able to perform whatever it was you wanted to prevent?Thanks for reply. Yes it is allowing to delete with the rule I applied and posted here as well. I might be missing something in the rules but it is allowing. It might be because I have admin rights on my PC. Thanks.
-
Yeah.. That's good idea. The only concern is that folder can't be deleted but other containts of the folder can be deleted one by one. Anyway, this solution might resolve my problem. Thanks.
The only thing you can do is be religious about using try/catch blocks and be ready with exception handling for when the user inevitably tries something stupid. I don't know the nature of your code, so you're going to have to evaluate what's necessary on a case-by-case basis.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Thanks for reply. Yes it is allowing to delete with the rule I applied and posted here as well. I might be missing something in the rules but it is allowing. It might be because I have admin rights on my PC. Thanks.