VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

VB.NET Source Code



Can Write To

Does the current user have permission to write to the specified directory?

Submitted By: AdamSpeight2008
Actions:
Rating:
Views: 1,901

Language: VB.NET

Last Modified: July 7, 2008

Snippet


  1. Public Function CanWriteTo(ByVal DirectoryPath As String) As Boolean
  2.  Dim dirInfo As System.IO.DirectoryInfo = FileIO.FileSystem.GetDirectoryInfo(DirectoryPath)
  3.  Dim currentUser As System.Security.Principal.WindowsIdentity = System.Security.Principal.WindowsIdentity.GetCurrent()
  4.  Dim currentPrinciple As System.Security.Principal.WindowsPrincipal = System.Threading.Thread.CurrentPrincipal
  5.  Dim acl As System.Security.AccessControl.AuthorizationRuleCollection = dirInfo.GetAccessControl().GetAccessRules(True, True, GetType(System.Security.Principal.SecurityIdentifier))
  6.  Dim currentRule As System.Security.AccessControl.FileSystemAccessRule
  7.  Dim denyread As Boolean = False
  8.  Dim allowread As Boolean = False
  9.  For x As Integer = 0 To acl.Count - 1
  10.   currentRule = acl(x)
  11.   If currentUser.User.Equals(currentRule.IdentityReference) Or currentPrinciple.IsInRole(currentRule.IdentityReference) Then
  12.    If currentRule.AccessControlType.Equals(Security.AccessControl.AccessControlType.Deny) Then
  13.     If (currentRule.FileSystemRights And Security.AccessControl.FileSystemRights.Write) = Security.AccessControl.FileSystemRights.Write Then denyread = True
  14.    Else
  15.     If currentRule.AccessControlType.Equals(AccessControlType.Allow) Then allowread = True
  16.    End If
  17.   End If
  18.  Next
  19.  If allowread And Not (denyread) Then
  20.   Return True
  21.  Else
  22.   Return False
  23.  End If
  24. End Function

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.