Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Folder Permissions using VB.NET

Status
Not open for further replies.

pkirill

Technical User
Jun 15, 2002
134
0
0
US
I have a small vb.net app that creates a set of project directories and preloads them with some common forms. It has come to the point where I need to limit permissions on the root folder, but open permissions on the sub folders - like this:

G:\ Drive - Everyone = Read & Execute
>>Root Folder - Everyone = Read & Execute
>>>>Subfolder1 - Everone = Full Permissions
>>>>Subfolder2 - Everone = Full Permissions

I'm hoping someone can show me how to do this with my limited programming skills in VB.NET as my attempts have failed.

Thanks!
 
I'm trying to do the same thing except I want the permission to persist down into subfolders which I haven't figured out yet.

Code:
Imports System
Imports System.IO
Imports System.Security.AccessControl

Module DirectoryExample

    Sub Main()
        Try
            Dim DirectoryName As String = "TestDirectory"

            Console.WriteLine("Adding access control entry for " + DirectoryName)

            ' Add the access control entry to the directory.
            AddDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Removing access control entry from " + DirectoryName)

            ' Remove the access control entry from the directory.
            RemoveDirectorySecurity(DirectoryName, "MYDOMAIN\MyAccount", FileSystemRights.ReadData, AccessControlType.Allow)

            Console.WriteLine("Done.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub


    ' Adds an ACL entry on the specified directory for the specified account.
    Sub AddDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
        ' Create a new DirectoryInfoobject.
        Dim dInfo As New DirectoryInfo(FileName)

        ' Get a DirectorySecurity object that represents the 
        ' current security settings.
        Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        dSecurity.AddAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        dInfo.SetAccessControl(dSecurity)

    End Sub


    ' Removes an ACL entry on the specified directory for the specified account.
    Sub RemoveDirectorySecurity(ByVal FileName As String, ByVal Account As String, ByVal Rights As FileSystemRights, ByVal ControlType As AccessControlType)
        ' Create a new DirectoryInfo object.
        Dim dInfo As New DirectoryInfo(FileName)

        ' Get a DirectorySecurity object that represents the 
        ' current security settings.
        Dim dSecurity As DirectorySecurity = dInfo.GetAccessControl()

        ' Add the FileSystemAccessRule to the security settings. 
        dSecurity.RemoveAccessRule(New FileSystemAccessRule(Account, Rights, ControlType))

        ' Set the new access settings.
        dInfo.SetAccessControl(dSecurity)

    End Sub
End Module
This is from the Help. It works but you have to go into the Advanced Security Settings to see it.

I'm trying to figure out what AuditRules do.
 
I'm using VS 2003 - and I don't have this: "Imports System.Security.AccessControl" Any ideas what I'm missing?
 
Hey,

Did you ever get any father with this? I've made some progress - figured out how to kill inheritance on my sub folders, but maintain in on the "sub-sub" folders. Saw you other post - mine's a CAD related this as well.

Using this will kill inheritance but copy the current ACL to the sub folders:
Code:
dSecurity.SetAccessRuleProtection(True, True)

If you change the second true to a false, it will not copy any rights assignments and you can programmatically add them back. However, I'm still trying to change the Users permissions on the first pass...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top