views:

433

answers:

0

Now I use the following code:

    Public Function SetACL(ByVal filename As String, ByVal account As String, ByVal sender As Object, ByVal e As System.EventArgs) As Boolean
        Try
            Dim rule As FileSystemAccessRule = New FileSystemAccessRule(account, FileSystemRights.Write, AccessControlType.Allow)
            Dim fp As PermissionSet = New PermissionSet(Permissions.PermissionState.Unrestricted)
            fp.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read, filename))
            fp.AddPermission(New FileIOPermission(FileIOPermissionAccess.Write, filename))
            fp.AddPermission(New FileIOPermission(FileIOPermissionAccess.PathDiscovery, filename))
            fp.Assert()
            Dim di As DirectoryInfo = New DirectoryInfo(Path.GetDirectoryName(filename))
            SetACL = False
            Dim security As DirectorySecurity = di.GetAccessControl(AccessControlSections.Access)
            security.ModifyAccessRule(AccessControlModification.Add, rule, SetACL)
            di.SetAccessControl(security)
Return SetACL
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Set Security Sub", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            Finalize()
        End Try

    End Function

The Error of 'Attempted to perform an unauthorized operation' comes when i'm trying to execute the instraction

Dim security As DirectorySecurity = di.GetAccessControl(AccessControlSections.Access)

Please if anybody knows why that error comes here to respond