PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Setting file and folder permissions-VBForums
Results 1 to 11 of 11

Thread: Setting file and folder permissions

Threaded View

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Setting file and folder permissions

    There are a few examples of this already on the web but most of them are over complicated and dont just give you a simple example so I thought it might be worth writing one here.

    Basically this code can be used to grant or restrict access for a specific user to a folder (see post #6 in this thread for example of setting permissions on a file instead of a folder). There are several things that you can play around with in this example to modify the effects (e.g deny permission instead of granting permission, modify the inheritance of the new permission, change the specific permissions issued etc etc) but if you run it as it is then it will grant the user Modify access to the folder and this permission will be inherited by any child objects within the folder. The new permission will also just be added to the folder's permission list, it will not replace the permissions already on the folder - If you want to completely remove all of the existing permissions on the folder when you add this new permission then you uncomment the line that is commented out near the end of the code.

    vb Code:
    1. 'At the top of your code
    2. Imports System.Security.AccessControl
    3.  
    4.  
    5. Dim FolderPath As String = "C:\TestingFolder" 'Specify the folder here
    6. Dim UserAccount As String = "MYDOMAIN\someuser" 'Specify the user here
    7.  
    8. Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(FolderPath)
    9. Dim FolderAcl As New DirectorySecurity
    10. FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))
    11. 'FolderAcl.SetAccessRuleProtection(True, False) 'uncomment to remove existing permissions
    12. FolderInfo.SetAccessControl(FolderAcl)

    This line is the main place where you might want to modify things to change the behavior of the permissions:
    vb Code:
    1. FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)
    Hopefully it is fairly obvious which parts of that line you need to change to get the desired effect that you want. For example if you want to just grant the user Read access instead of Modify then the line might look like this:
    vb Code:
    1. FolderAcl.AddAccessRule(New FileSystemAccessRule(UserAccount, FileSystemRights.ReadAndExecute, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow))


    If you want to set permissions for files instead of folders, just use the same technique but use the FileSecurity class instead of DirectorySecurity.



    Hope that helps someone and let me know if you have any questions.
    Last edited by chris128; Jun 4th, 2010 at 05:52 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width