How to get simple yes/no if user has read/write permission on a directory?

teleute

Member
Joined
May 7, 2013
Messages
7
Programming Experience
10+
Subject pretty much says it. I'm looking for just a straightforward yes/no as to whether a user has write permission on a specific directory. I'm catching the exceptions if they try to write there and they can't, but I'd prefer to have a warning up front as well if they don't have permission to the directory, before they do a bunch of stuff. I've been playing with a few different things (like GetAccessControl.GetAccessRules), but haven't found anything that works yet.

Thanks!
 
Try to write something and catch the exception yourself, at the moment you want to test for it.

Private Function IsPathWritable(ByVal strPath As String) As Boolean
    IsPathWritable = True
    If Not Directory.Exists(strPath) Then
        IsPathWritable = False
    Else
        Try
            File.Create(strPath & "\WriteTest.txt")
        Catch ex As IOException
            IsPathWritable = False
        Finally
            If File.Exists(strPath & "\WriteTest.txt") And IsPathWritable Then File.Delete(strPath & "\WriteTest.txt")
        End Catch
    End If
End Function
 
Try to write something and catch the exception yourself, at the moment you want to test for it.

<code snipped>

I thought about that, but it feels more like a workaround than the real way. Is that really the only way to do it? It feels like there should be a method to call or something...

ETA: I tried this, and it creates the file just fine, but when it tries to delete it's throwing an exception that it can't access the file because it is being used by another process. I'll keep poking, though. Thanks!
(Also, I had to change the "End Catch" line to an "End Try" - it didn't like it the other way. I don't think that's the problem, but I'm not 100% sure...)
 
Last edited:
So I ended up having to change it to this:

    Public Shared Function IsPathWritable(ByVal strPath As String) As Boolean
        IsPathWritable = True
        If Not Directory.Exists(strPath) Then
            IsPathWritable = False
        Else
            Try
                Dim fs As FileStream = File.Create(strPath & "\WriteTest.txt")
                fs.Close()
            Catch ex As IOException
                IsPathWritable = False
            Finally
                If File.Exists(strPath & "\WriteTest.txt") And IsPathWritable Then File.Delete(strPath & "\WriteTest.txt")
            End Try
        End If
    End Function


Not sure if that's how I *should* be doing it, but that's what seemed to be needed to delete the file. Is there a better way, or does that seem correct?

Thanks!
 
With File.Create method you can specify FileOptions.DeleteOnClose.

As for checking permissions, you may not be allowed to do that if you don't have access to the folder, GetAccessControl will for example throw exception if folder is readonly. If you can write to a folder you may still not have high enough security clearance to query permissions.
 
With File.Create method you can specify FileOptions.DeleteOnClose.

As for checking permissions, you may not be allowed to do that if you don't have access to the folder, GetAccessControl will for example throw exception if folder is readonly. If you can write to a folder you may still not have high enough security clearance to query permissions.

Awesome - thanks! So that seems to cut out the need for the Finally block...

One thing, though - since I'm specifically testing for access here (not whether it exists, etc...I do that elsewhere) I think it's actually an UnauthorizedAccessException I need to catch, not IO - does that seem right?

Thanks again!
 
Based on documentation I would say so, yes. IOException is a base type that includes other exceptions the Create method may throw, such as PathTooLongException and DirectoryNotFoundException.
 
Back
Top