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(Direc toryName, "Users", FileSystemRights.FullContr ol, AccessControlType.Allow)
ElseIf strType = "Remove" Then
' Remove the access control entry from the directory.
RemoveDirectorySecurity(Di rectoryNam e, "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.Container Inherit
Dim pPropogate As PropagationFlags = PropagationFlags.InheritOn ly
' 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(Ne w FileSystemAccessRule(Accou nt, Rights, iFlag, pPropogate, ControlType))
' Set the new access settings.
dInfo.SetAccessControl(dSe curity)
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.FullContr ol, AccessControlType.Allow)
ElseIf strType = "Remove" Then
' Remove the access control entry from the directory.
RemoveFileSecurity(sFileNa me, "Users", FileSystemRights.FullContr ol, 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(file Name)
' Add the FileSystemAccessRule to the security settings.
Dim accessRule As FileSystemAccessRule = _
New FileSystemAccessRule(accou nt, rights, controlType)
fSecurity.AddAccessRule(ac cessRule)
' Set the new access settings.
File.SetAccessControl(file Name, fSecurity)
End Sub
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(Direc
ElseIf strType = "Remove" Then
' Remove the access control entry from the directory.
RemoveDirectorySecurity(Di
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
' Create a new DirectoryInfoobject.
Dim dInfo As New DirectoryInfo(FileName)
Dim iFlag As InheritanceFlags = InheritanceFlags.Container
Dim pPropogate As PropagationFlags = PropagationFlags.InheritOn
' 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(Ne
' Set the new access settings.
dInfo.SetAccessControl(dSe
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,
ElseIf strType = "Remove" Then
' Remove the access control entry from the directory.
RemoveFileSecurity(sFileNa
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(file
' Add the FileSystemAccessRule to the security settings.
Dim accessRule As FileSystemAccessRule = _
New FileSystemAccessRule(accou
fSecurity.AddAccessRule(ac
' Set the new access settings.
File.SetAccessControl(file
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.