Blog Archive

Search Blog

Blog List

Contact Me






Enter the code shown above in the box below
Send

 

Announcements

Recommended Websites

Events

Event StartEvent EndTitle

Recommended Reading

Most recent blog entries

Aug 29

Written by: Darren McLeod
Saturday, August 29, 2009 10:03 AM 

I could not find any function in .NET that just straight out does this.  Further more I could not find any code on the web that explained what would be the best way to go about it.  Finally I found some code here:

 

http://www.dreamincode.net/code/snippet2120.htm

 

that checks if you could write to a file and I modified it to check if you could read:

 

    Public Shared Function CanReadFile(ByVal fileNameWithPath As String) As Boolean

        Dim currentUser As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()

        Dim currentPrinciple As System.Security.Principal.WindowsPrincipal = System.Threading.Thread.CurrentPrincipal

        Dim acl As System.Security.AccessControl.AuthorizationRuleCollection = System.IO.File.GetAccessControl(fileNameWithPath).GetAccessRules(True, True, GetType(System.Security.Principal.SecurityIdentifier))

        Dim denyread As Boolean = False

        Dim allowread As Boolean = False

        For Each currentRule As System.Security.AccessControl.FileSystemAccessRule In acl

            If currentUser.User.Equals(currentRule.IdentityReference) Or currentPrinciple.IsInRole(currentRule.IdentityReference) Then

                If currentRule.AccessControlType.Equals(System.Security.AccessControl.AccessControlType.Deny) Then

                    If (currentRule.FileSystemRights And System.Security.AccessControl.FileSystemRights.Read) = System.Security.AccessControl.FileSystemRights.Read Then denyread = True

                Else

                    If currentRule.AccessControlType.Equals(AccessControlType.Allow) Then allowread = True

                End If

            End If

        Next

        If allowread And Not (denyread) Then

            Return True

        Else

            Return False

        End If

    End Function

Tags:

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
Enter the code shown above in the box below
Add Comment   Cancel