Ich verwende dazu diese Klasse hier -> Checking for directory and file write permissions in .NET
''' <summary>
''' Checking for directory and file write permissions
''' </summary>
''' <remarks>https://stackoverflow.com/a/22020271</remarks>
Public Class CurrentUserSecurity
Private ReadOnly _currentUser As WindowsIdentity
Private ReadOnly _currentPrincipal As WindowsPrincipal
Public Sub New()
_currentUser = WindowsIdentity.GetCurrent()
_currentPrincipal = New WindowsPrincipal(WindowsIdentity.GetCurrent())
End Sub
Public Function HasAccess(directory As IO.DirectoryInfo, right As _
Security.AccessControl.FileSystemRights) As Boolean
' Get the collection of authorization rules that apply to the directory.
Try
Dim acl As Security.AccessControl.AuthorizationRuleCollection = _
directory.GetAccessControl().GetAccessRules(True, True, GetType( _
SecurityIdentifier))
Return HasFileOrDirectoryAccess(right, acl)
Catch UAEx As UnauthorizedAccessException
Return False
End Try
End Function
Public Function HasAccess(file As IO.FileInfo, right As _
Security.AccessControl.FileSystemRights) As Boolean
' Get the collection of authorization rules that apply to the file.
Try
Dim acl As Security.AccessControl.AuthorizationRuleCollection = _
file.GetAccessControl().GetAccessRules(True, True, GetType( _
SecurityIdentifier))
Return HasFileOrDirectoryAccess(right, acl)
Catch UAEx As UnauthorizedAccessException
Return False
End Try
End Function
Private Function HasFileOrDirectoryAccess(right As _
Security.AccessControl.FileSystemRights, acl As _
Security.AccessControl.AuthorizationRuleCollection) As Boolean
Dim allow As Boolean = False
Dim inheritedAllow As Boolean = False
Dim inheritedDeny As Boolean = False
For i As Integer = 0 To acl.Count - 1
Dim currentRule As Security.AccessControl.FileSystemAccessRule = _
DirectCast(acl(i), Security.AccessControl.FileSystemAccessRule)
' If the current rule applies to the current user.
If _currentUser.User.Equals(currentRule.IdentityReference) OrElse _
_currentPrincipal.IsInRole(DirectCast( _
currentRule.IdentityReference, SecurityIdentifier)) Then
If currentRule.AccessControlType.Equals( _
Security.AccessControl.AccessControlType.Deny) Then
If (currentRule.FileSystemRights And right) = right Then
If currentRule.IsInherited Then
inheritedDeny = True
Else
' Non inherited "deny" takes overall precedence.
Return False
End If
End If
ElseIf currentRule.AccessControlType.Equals( _
Security.AccessControl.AccessControlType.Allow) Then
If (currentRule.FileSystemRights And right) = right Then
If currentRule.IsInherited Then
inheritedAllow = True
Else
allow = True
End If
End If
End If
End If
Next
If allow Then
' Non inherited "allow" takes precedence over inherited rules.
Return True
End If
Return inheritedAllow AndAlso Not inheritedDeny
End Function
End Class Aufrufen tu ich es dann so:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) _
Handles Button1.Click
Dim sPath As String = "F:\Temp"
If IO.Directory.Exists(sPath) Then
Dim CUS As New CurrentUserSecurity
Dim test As Boolean = CUS.HasAccess(New IO.DirectoryInfo(sPath), _
Security.AccessControl.FileSystemRights.Write)
MessageBox.Show($"Sie {If(test, "haben", "haben KEIN")} Schreibzugriff" & _
"auf {sPath}.")
Else
MessageBox.Show($"Kann {sPath} nich finden.")
End If
End Sub |