accessing folders

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
short and simple i modified paszt and jm's search algorithm that's been posted on this forum several times to search for files in a folder (and it's sub folders) here's the sub that runs in it's own thread:
VB.NET:
    Private Sub GetArchive(ByVal Src As String)
        Dim Files() As String = Directory.GetFileSystemEntries(Src)
        For Each element As String In Files
            If Directory.Exists(element) = True Then
                'if the current FileSystemEntry is a directory, call this function recursively
                Call GetArchive(element)
            Else
                'the current FileSystemEntry is a file so just copy it
                Select Case mblnNewArchive
                    Case True
                        If element.EndsWith(".m3u") Then
                            ArchiveList.Add(element)
                        End If
                    Case False
                        If element.EndsWith(".mp3") OrElse element.EndsWith(".mp2") OrElse element.EndsWith(".wma") OrElse element.EndsWith(".wmv") OrElse element.EndsWith(".ogg") OrElse element.EndsWith(".m3u") OrElse element.EndsWith(".pls") Then
                            ArchiveList.Add(element)
                        End If
                End Select
            End If
        Next element
    End Sub


the problem is that if i use this with the root drive on my HDD (such as "c:\" or "d:\") the try catch block catches an error saying that it doesnt have the rights to access "d:\systemvolumeinformation" of which i dont need it to access that folder, so how would i modify it to check each folder and see if the program has access to it? if the program doesnt have access to the folder then i need the program to skip that folder
 
Try this line (the folder recursion), or the whole thing... probably everything that happens behind a Try any level is catched and slowed down
VB.NET:
Try
  Call GetArchive(element)
Catch ex As Exception
End Try
 
i think this is security issue because .net scans folders from root upward to check if they exist. If asp.net user does not have Read Attribute rights on root drive it will throw an error. Most of the hosting providers will not give you that rights on their server so this is very serious bug in net. There is other method of using com but that app will only run in full trust.

try to use directory.createdirectory method and it will throw an error (not on local because you probably have asp.net user read rights on root drive.
 
ahh, using a 2nd try/catch block, i hadnt thought of that it does work too

although i was looking for something like:
If Directory.HasAccess Then Call GetArchive(element) but obviously it wouldnt be that easy
the try/catch works and speed isnt an issue so i'll use that thanx
 
Back
Top