用什么代替 SecurityManager.IsGranted?

What to use instead of SecurityManager.IsGranted?

我没有找到我要问的问题。

我想知道用户是否有access/permission删除给定文件或文件夹的权限

我正在使用这个:

Private Function UserHasPermissionsToSource(ByVal source As String) As Boolean    
    Dim writePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, source)
    Return SecurityManager.IsGranted(writePermission)
End Function

但是 isGranted 已过时,我不确定用什么来替换它。这是我目前拥有的,有人可以确认这是否正确吗?

Private Function UserHasPermissionsToSource(ByVal source As String) As Boolean
    'Get the permissions of the file/folder
    Dim writePermission As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, source)

    'Create your permission set and add file permissions for write
    Dim permissionSet As PermissionSet = New PermissionSet(PermissionState.None)
    permissionSet.AddPermission(writePermission)

    'checks permissions????
    If permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet) Then
        Return True
    End If

    Return False
End Function

对于相同的结果似乎需要更多的工作。

以上不正确,我找到了解决办法。

Private Function GetFileAccessControl(ByVal source As String) As String

        Dim denied As Boolean = False
        Dim allowed As Boolean = False

        'Gets the current User
        Dim wid As WindowsIdentity = WindowsIdentity.GetCurrent()

        Try
            Dim arc As AuthorizationRuleCollection = GetAuthorizationRuleCollection(source)
            Dim ars As IList(Of FileSystemAccessRule) = New List(Of FileSystemAccessRule)(arc.OfType(Of FileSystemAccessRule))
            Dim widgs As IList(Of IdentityReference) = New List(Of IdentityReference)

            For Each g As SecurityIdentifier In wid.Groups
                If g.IsAccountSid() Then
                    widgs.Add(g)
                End If
            Next

            'User not inherited rules
            For Each rule As FileSystemAccessRule In (From r In ars Where r.IdentityReference.Equals(wid.User) AndAlso Not r.IsInherited)
                denied = denied Or DeniesWriteAccess(rule)
                allowed = allowed Or AllowsWriteAccess(rule)
            Next

            For Each rule As FileSystemAccessRule In (From r In ars Where r.IdentityReference.Equals(wid.User) AndAlso r.IsInherited)
                denied = denied Or DeniesWriteAccess(rule)
                allowed = allowed Or AllowsWriteAccess(rule)
            Next

            For Each rule As FileSystemAccessRule In (From r In ars Where widgs.Contains(r.IdentityReference) AndAlso Not r.IsInherited)
                denied = denied Or DeniesWriteAccess(rule)
                allowed = allowed Or AllowsWriteAccess(rule)
            Next

            For Each rule As FileSystemAccessRule In (From r In ars Where widgs.Contains(r.IdentityReference) AndAlso r.IsInherited)
                denied = denied Or DeniesWriteAccess(rule)
                allowed = allowed Or AllowsWriteAccess(rule)
            Next
        Catch ex As UnauthorizedAccessException

        End Try

        If Not denied AndAlso allowed Then
            Return True
        End If

        Return False
    End Function

    Private Function GetAuthorizationRuleCollection(ByVal source As String) As AuthorizationRuleCollection

        'Gets the current User
        Dim wid As WindowsIdentity = WindowsIdentity.GetCurrent()
        Dim arc As AuthorizationRuleCollection
        arc = Nothing

        If (Directory.Exists(source)) Then
            Dim di As DirectoryInfo = New DirectoryInfo(source)
            Dim acl As DirectorySecurity = di.GetAccessControl()
            arc = acl.GetAccessRules(True, True, GetType(SecurityIdentifier))
        ElseIf File.Exists(source) Then
            Dim fi As FileInfo = New FileInfo(source)
            Dim acl As FileSecurity = fi.GetAccessControl()
            arc = acl.GetAccessRules(True, True, GetType(SecurityIdentifier))
        End If

        Return arc
    End Function

    Private Function AllowsWriteAccess(rule As FileSystemAccessRule) As Boolean
        If rule.AccessControlType = AccessControlType.Allow AndAlso
                (rule.FileSystemRights.HasFlag(FileSystemRights.Write) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.CreateDirectories) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.CreateFiles)) Then
            Return True
        End If
        Return False
    End Function

    Private Function DeniesWriteAccess(rule As FileSystemAccessRule) As Boolean
        If rule.AccessControlType = AccessControlType.Deny AndAlso
                (rule.FileSystemRights.HasFlag(FileSystemRights.Write) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.WriteData) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.CreateDirectories) OrElse
                rule.FileSystemRights.HasFlag(FileSystemRights.CreateFiles)) Then
            Return True
        End If
        Return False
    End Function