[2005] Check Folder exists and permissions
Guys,
I am trying to create a function that will take a FolderPath as an arguement, then check to see if ...
1 - The folder exists
2 - The user have write access
I need the function to return true if the folder exists and the user has write perms, otherwise false.
This is what I have so far but it doesn't seem to be doing much... its returning true if the folder doesn't exist.
Thanks
Bob
VB Code:
Public Function CheckFilePermissions(ByVal CheckPath As String, ByVal Foldername As String) As Boolean
Dim permFileIO As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, CheckPath)
Try
permFileIO.Demand()
Return True
Catch ex As Exception
MessageBox.Show("The " & Foldername & "for this contract does not exist," & _
vbCrLf & "or you do not have the correct permissions to write to it." & _
vbCrLf & vbCrLf & "Please contact the IT Helpdesk if you need help resolving this issue. Quote PWB003" & _
vbCrLf & vbCrLf & CheckPath, "Document Folder Error (PWB003)", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
Re: [2005] Check Folder exists and permissions
I have always used this function i created to verify if a directory exists. Do this before you check permissions.
VB Code:
Public Function VerifyDirectory(ByVal sDirectory As String) As Boolean
'Checks to ensure the directory actually exists
Try
sDirectory = sDirectory.Trim & ""
If sDirectory = "" Then Return False
'Returns true if the directory exists
Dim bDoesItExist As Boolean = System.IO.Directory.Exists(sDirectory)
Return bDoesItExist
Catch
Return False
End Try
End Function
Re: [2005] Check Folder exists and permissions
You might have to use the FileIO to check that the folder exists, then check permissions. Even if the folder doesn't exist, it's not going to trip an error and go into your catch statement.
Re: [2005] Check Folder exists and permissions
My.Computer.FileSystem.DirectoryExists("C:\TestDirectory") will check if a folder exists. That gets you halfway there...
Re: [2005] Check Folder exists and permissions
As far as the permissions go, I am not all too sure what is going on. I wrote a little function that checks if the directory exists, and then checks to see if the readonly attribute is set, but I am getting conflicting values, might be due to something wrong in the Bitwise AND?. Some directories that are not readonly still return False, but some, like My Documents folder under the current user return true, as it should. If someone can help explain or offer an alternative solution? Perhaps this checks user permissions and not group permissions?
VB Code:
'function
Private Function WriteAccess(ByVal Directory As String) As Boolean
'strips end \ character that is returned on some directories
'(like root directories) that cause DirectoryExists function to give error
If Directory.EndsWith("\"c) Then
Directory = Directory.Substring(0, Directory.Length - 1)
End If
If My.Computer.FileSystem.DirectoryExists(Directory) Then
Dim Attributes As System.IO.FileAttributes = My.Computer.FileSystem.GetFileInfo(Directory).Attributes
'BitWise to check if readonly attribute is set - problem here?
If (Attributes And IO.FileAttributes.ReadOnly) <> 0 Then
Console.WriteLine("True - Exists and no readonly - " & Directory)
Return True
Else
Console.WriteLine("False - ReadOnly - " & Directory)
Return False
End If
Else
Console.WriteLine("False - Directory does not exist - " & Directory)
Return False
End If
End Function
'example of usage
Dim Dialog As New FolderBrowserDialog
Dialog.ShowDialog()
MessageBox.Show(WriteAccess(Dialog.SelectedPath).ToString)