I've got a little code that creates some subfolders and then applies some permissions. I've got it working so it creates the first two folders, applies permissions correctly to the first folder but then stops. if I run the same code again specifying the same subfolders, it creates all the folders and applies the permissions correctly. I'd prefer not to have to run the program twice - so any assistance would be appreciated! (Code is below)
Code:
'Create initial subfolder - this folder inherits permissions from a parent folder.
Private Sub CreateProjectFolder(ByVal strProjectPath As String)
My.Computer.FileSystem.CreateDirectory(strProjectPath)
Dim dInfo As New DirectoryInfo(strProjectPath)
Dim ruleF As FileSystemAccessRule
ruleF = New FileSystemAccessRule("builtin\users", FileSystemRights.ReadAndExecute, InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl
dSecurity.AddAccessRule(ruleF)
dInfo.SetAccessControl(dSecurity)
End Sub
Code:
'this code uses an array of subfolder names to create and run the AddDirectorySecurity sub...
Private Sub CreateCommonFolders(ByVal strProjectPath As String)
Dim arFolderList() As String = {"\_Data", "\_Docs", "\_Financial", "\_PM", "\_Specs", "\_Submissions"}
Dim strSubfolder As String
For Each strSubfolder In arFolderList
Directory.CreateDirectory(strProjectPath & strSubfolder)
AddDirectorySecurity(strProjectPath, strSubfolder)
Next strSubfolder
end sub
Code:
'This is the AddDirectorySecurity sub. It seems to hang when it gets to the AddAccessRule(ruleF) line...
Sub AddDirectorySecurity(ByVal strProjectPath As String, ByVal strSubFolder As String)
Dim blnDirExists As Boolean = False
Dim dInfo As New DirectoryInfo(strProjectPath & strSubFolder)
Dim ruleF As FileSystemAccessRule
ruleF = New FileSystemAccessRule("builtin\users", FileSystemRights.FullControl, InheritanceFlags.ContainerInherit + InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)
Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl
dSecurity.AddAccessRule(ruleF)
dInfo.SetAccessControl(dSecurity)
End Sub