Solved

How do I set folder permissions using VB.Net

Posted on 2011-03-21
3
418 Views
Last Modified: 2012-05-11
The code below is creating personal folders for users in the following format:
\\server\school\username
The server is always the same but the school and username change. The folders are being created exactly the way I want except permissions are not being set on the folders the way I would like. What I would like is for the permissions to be as follows:
domain\username - modify
domain\domain admins - full control
domain\callcenter - full control
nas\administrators - full control

The final result for a folder would be \\nas\school\tsmith1 with permissions being modify for tsmith1 and Full Control for domain admins, callcenter and nas administrators.
If Len(ex1) > 0 Then
            If (DTM.Context("LocationRootFolder") IsNot Nothing) Then
                If (DTM.Context("LocationMap") IsNot Nothing) Then
                    Dim filter1 As String = DTM.Context("LocationRootFolder")
                    Dim locations1 As System.Collections.Hashtable
                    locations1 = DTM.Context("LocationMap")
                    If Len(locations1(ex1)) > 0 Then
                        homeDir = String.Format(filter1, locations1(ex1), existingSam1)
                        Dim CurrentPath as String = homeDir
 Dim UserName1 As String = "domain\" & existingSam1
 Dim UserName2 As String = "domain\Domain Admins"
 Dim UserName3 As String = "domain\CallCenter"
 Dim UserName4 As String = "NAS\Administrators"
 
 Dim dInfo As New DirectoryInfo(CurrentPath)
 Dim dSecInfo As DirectorySecurity = dInfo.GetAccessControl(AccessControlSections.Access)
 Dim myRuleValue As Integer = 0
 
 myRuleValue =  FileSystemRights.Modify   'Add your wanted Access here
 Dim myRule as FileSystemAccessRule
 myRule = New FileSystemAccessRule(UserName1, myRuleValue, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow)
 dSecInfo.AddAccessRule(myRule)
 dSecInfo.SetAccessRule(myRule)
 dInfo.SetAccessControl(dSecInfo)
                    End If

Open in new window

0
Comment
Question by:dmillerpps
3 Comments
 
LVL 4

Accepted Solution

by:
Tuyau2poil earned 500 total points
ID: 35184415
your code is Ok. just add acl for each user.
try this :

If Len(ex1) > 0 Then
            If (DTM.Context("LocationRootFolder") IsNot Nothing) Then
                If (DTM.Context("LocationMap") IsNot Nothing) Then
                    Dim filter1 As String = DTM.Context("LocationRootFolder")
                    Dim locations1 As System.Collections.Hashtable
                    locations1 = DTM.Context("LocationMap")
                    If Len(locations1(ex1)) > 0 Then
                        homeDir = String.Format(filter1, locations1(ex1), existingSam1)
                        Dim CurrentPath as String = homeDir
 Dim UserName1 As String = "domain\" & existingSam1
 Dim UserName2 As String = "domain\Domain Admins"
 Dim UserName3 As String = "domain\CallCenter"
 Dim UserName4 As String = "NAS\Administrators"
 
 Dim dInfo As New DirectoryInfo(CurrentPath)
 Dim dSecInfo As DirectorySecurity = dInfo.GetAccessControl()

 dSecInfo.AddAccessRule(New FileSystemAccessRule(UserName1, FileSystemRights.Modify, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow))
 dInfo.SetAccessControl(dSecInfo)

 dSecInfo.AddAccessRule(New FileSystemAccessRule(UserName2, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow))
 dInfo.SetAccessControl(dSecInfo)
        
 dSecInfo.AddAccessRule(New FileSystemAccessRule(UserName3, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow))
 dInfo.SetAccessControl(dSecInfo)
        
 dSecInfo.AddAccessRule(New FileSystemAccessRule(UserName4, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit Or InheritanceFlags.ObjectInherit Or InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow))
 dInfo.SetAccessControl(dSecInfo)

End If

Open in new window

0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

OfficeMate Freezes on login or does not load after login credentials are input.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now