Solved

How do I set folder permissions using VB.Net

Posted on 2011-03-21
3
424 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 36
vb.net help 4 29
paging file usage  > 95% for 30 min about 12 30
ASP/VB email question 4 34
I was supporting a handful of Windows 2008 (non-R2) 2 node clusters with shared quorum disks. Some had SQL 2008 installed and some were just a vendor application that we supported. For the purposes of this article it doesn’t really matter which so w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

770 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