Link to home
Start Free TrialLog in
Avatar of baeriali
baeriali

asked on

Set File/Folder permissions

Having trouble setting Folder permissions for the USERS account.
Do not get any error messages back but it does not seem to be working.
Any help would be apreciated.

Currently using the following code to set File/Folder permissions.

Public Function Set_DirectoryAccess(ByVal strType As String, ByVal strDirectoryName As String) As Integer

        Try
            Dim DirectoryName As String = strDirectoryName

            If strType = "Add" Then
                ' Add the access control entry to the directory.
                AddDirectorySecurity(DirectoryName, "Users", FileSystemRights.FullControl, AccessControlType.Allow)

            ElseIf strType = "Remove" Then
                ' Remove the access control entry from the directory.
                RemoveDirectorySecurity(DirectoryName, "Users", FileSystemRights.Write, AccessControlType.Allow)
            End If
            Set_DirectoryAccess = 1
        Catch e As Exception
            MsgBox(e.ToString)
            Set_DirectoryAccess = -1
        End Try
    End Function

' Adds an ACL entry on the specified directory for the specified account.
    Private 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)

        Dim iFlag As InheritanceFlags = InheritanceFlags.ContainerInherit
        Dim pPropogate As PropagationFlags = PropagationFlags.InheritOnly

        ' 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, iFlag, pPropogate, ControlType))

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

Public Function Set_FileAccess(ByVal strType As String, ByVal strFileName As String) As Integer

        Try
            Dim sFileName As String = strFileName

            If strType = "Add" Then
                ' Add the access control entry to the directory.
                AddFileSecurity(sFileName, "Users", FileSystemRights.FullControl, AccessControlType.Allow)

            ElseIf strType = "Remove" Then
                ' Remove the access control entry from the directory.
                RemoveFileSecurity(sFileName, "Users", FileSystemRights.FullControl, AccessControlType.Allow)
            End If
            Set_FileAccess = 1
        Catch e As Exception
            MsgBox(e.ToString)
            Set_FileAccess = -1
        End Try
    End Function

 ' Adds an ACL entry on the specified file for the specified account.
    Sub AddFileSecurity(ByVal fileName As String, ByVal account As String, _
        ByVal rights As FileSystemRights, ByVal controlType As AccessControlType)

        ' Get a FileSecurity object that represents the
        ' current security settings.
        Dim fSecurity As FileSecurity = File.GetAccessControl(fileName)

        ' Add the FileSystemAccessRule to the security settings.
        Dim accessRule As FileSystemAccessRule = _
            New FileSystemAccessRule(account, rights, controlType)

        fSecurity.AddAccessRule(accessRule)

        ' Set the new access settings.
        File.SetAccessControl(fileName, fSecurity)
    End Sub

ASKER CERTIFIED SOLUTION
Avatar of Jason Evans
Jason Evans
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of baeriali
baeriali

ASKER

Using Vista as an OS passing in the DomainName\Users returns an error. Using the code I listed does now seem to work. Not certain if it will work in all cases.

I appreciate your attempt to help solve my issue.

In this case since your solution did not work ... do I give you all the points or just a part of them.
Please let me know how you think the points should be alloted.

I have no problem giving them all to you since you were the only respondent to my question.