Attempted to perform an unauthorized operation

Posted by Lefteris Gkinis on Stack Overflow See other posts from Stack Overflow or by Lefteris Gkinis
Published on 2010-01-26T15:48:53Z Indexed on 2010/05/02 16:18 UTC
Read the original article Hit count: 201

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

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about filestream