Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Prevent access to directory

Prevent access to directory

Scheduled Pinned Locked Moved C#
questioncsharphelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maddie from Dartford
    wrote on last edited by
    #1

    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.

    C M 2 Replies Last reply
    0
    • M Maddie from Dartford

      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.

      C Offline
      C Offline
      Calla
      wrote on last edited by
      #2

      I think this can be achieved with the FileSystemAccessRule class (the creation of the rule(s) that is). Then you create a DirectorySecurity ojbect and use the AddAccessRule method to apply your access rule(s). Finally, create a DirectoryInfo object representing the folder and run the SetAccessControl method which takes your DirectorySecurity object as an argument. That's pretty much how I've done it in the past. Good luck! :)

      1 Reply Last reply
      0
      • M Maddie from Dartford

        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.

        M Offline
        M Offline
        Maddie from Dartford
        wrote on last edited by
        #3

        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.

        realJSOPR C 2 Replies Last reply
        0
        • M Maddie from Dartford

          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.

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • realJSOPR realJSOP

            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

            M Offline
            M Offline
            Maddie from Dartford
            wrote on last edited by
            #5

            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.

            realJSOPR 1 Reply Last reply
            0
            • M Maddie from Dartford

              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.

              C Offline
              C Offline
              Calla
              wrote on last edited by
              #6

              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?

              M 1 Reply Last reply
              0
              • C Calla

                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?

                M Offline
                M Offline
                Maddie from Dartford
                wrote on last edited by
                #7

                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.

                C 1 Reply Last reply
                0
                • M Maddie from Dartford

                  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.

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • M Maddie from Dartford

                    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.

                    C Offline
                    C Offline
                    Calla
                    wrote on last edited by
                    #9

                    I didn't check your use of flags that thoroughly, but it looked ok. But since you only add the deny rules for the Users group I would assume an Administrator is still able to delete the folder in question.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups