Results 1 to 5 of 5

Thread: [2005] Check Folder exists and permissions

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    [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:
    1. Public Function CheckFilePermissions(ByVal CheckPath As String, ByVal Foldername As String) As Boolean
    2.  
    3.         Dim permFileIO As FileIOPermission = New FileIOPermission(FileIOPermissionAccess.Write, CheckPath)
    4.         Try
    5.             permFileIO.Demand()
    6.             Return True
    7.         Catch ex As Exception
    8.             MessageBox.Show("The " & Foldername & "for this contract does not exist," & _
    9.             vbCrLf & "or you do not have the correct permissions to write to it." & _
    10.             vbCrLf & vbCrLf & "Please contact the IT Helpdesk if you need help resolving this issue. Quote PWB003" & _
    11.             vbCrLf & vbCrLf & CheckPath, "Document Folder Error (PWB003)", MessageBoxButtons.OK, MessageBoxIcon.Error)
    12.             Return False
    13.         End Try
    14.  
    15.     End Function
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool 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:
    1. Public Function VerifyDirectory(ByVal sDirectory As String) As Boolean
    2.         'Checks to ensure the directory actually exists
    3.         Try
    4.             sDirectory = sDirectory.Trim & ""
    5.             If sDirectory = "" Then Return False
    6.  
    7.             'Returns true if the directory exists
    8.             Dim bDoesItExist As Boolean = System.IO.Directory.Exists(sDirectory)
    9.             Return bDoesItExist
    10.         Catch
    11.             Return False
    12.         End Try
    13.     End Function
    ~Peter


  3. #3
    Fanatic Member
    Join Date
    Aug 2006
    Location
    Chicago, IL
    Posts
    514

    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.
    Warren Ayen
    Senior C# Developer
    DLS Software Studios (http://www.dlssoftwarestudios.com/)

    I use Microsoft Visual Studio 2005, 2008, working with Visual Basic and Visual C#
    Hey! If you like my post, or I solve your issue, please Rate Me!

  4. #4
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: [2005] Check Folder exists and permissions

    My.Computer.FileSystem.DirectoryExists("C:\TestDirectory") will check if a folder exists. That gets you halfway there...

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. 'function
    2.     Private Function WriteAccess(ByVal Directory As String) As Boolean
    3.         'strips end \ character that is returned on some directories
    4.         '(like root directories) that cause DirectoryExists function to give error
    5.         If Directory.EndsWith("\"c) Then
    6.             Directory = Directory.Substring(0, Directory.Length - 1)
    7.         End If
    8.  
    9.         If My.Computer.FileSystem.DirectoryExists(Directory) Then
    10.             Dim Attributes As System.IO.FileAttributes = My.Computer.FileSystem.GetFileInfo(Directory).Attributes
    11.             'BitWise to check if readonly attribute is set - problem here?
    12.             If (Attributes And IO.FileAttributes.ReadOnly) <> 0 Then
    13.                 Console.WriteLine("True - Exists and no readonly - " & Directory)
    14.                 Return True
    15.             Else
    16.                 Console.WriteLine("False - ReadOnly - " & Directory)
    17.                 Return False
    18.             End If
    19.         Else
    20.             Console.WriteLine("False - Directory does not exist - " & Directory)
    21.             Return False
    22.         End If
    23.     End Function
    24.  
    25. 'example of usage
    26.         Dim Dialog As New FolderBrowserDialog
    27.         Dialog.ShowDialog()
    28.         MessageBox.Show(WriteAccess(Dialog.SelectedPath).ToString)
    Last edited by gigemboy; Aug 16th, 2006 at 02:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width