psyfect
asked on
VB.Net Recurse Subdirectories (permissions?)
I am attempting to identify all subdirectories within a user-specified path on two Operating Systems, Vista and XP. I have two current options. The first one seems to be the best, as it tends to actually read the folders, my problem is that it is broken somehow, and not truly recursive:
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
For Each Fil In Fils
MsgBox(Fil.Name)
SearchFolders(Fil.FullName & "\")
Next
End Function
Preferably, I would like to have a functioning version of this code which actually acknowledges all subdirectories. Alternatively, however, I found different code which seems to work flawlessly and is extremely small. The problem, although, appears to be permissions.
The previous code will acknowledge paths below "C:\Users\" on a Vista machine (at least the ones found by the broken code). The following code, however, seems to not have permissions and consequently does not display anything:
Public Function RecursiveFolder(ByVal path As String) As Long
For Each fold As String In My.Computer.FileSystem.Get Directorie s(path, FileIO.SearchOption.Search AllSubDire ctories)
MsgBox(fold)
Next
End
If I were to use the above code on a path to an external hard drive, all subdirectories would be acknowledged. Though if I were to chose "C:\Users\" on Vista I get nothing. So while this code would be most efficient for an XP machine with no preset permissions, it is useless for my requirements.
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
For Each Fil In Fils
MsgBox(Fil.Name)
SearchFolders(Fil.FullName
Next
End Function
Preferably, I would like to have a functioning version of this code which actually acknowledges all subdirectories. Alternatively, however, I found different code which seems to work flawlessly and is extremely small. The problem, although, appears to be permissions.
The previous code will acknowledge paths below "C:\Users\" on a Vista machine (at least the ones found by the broken code). The following code, however, seems to not have permissions and consequently does not display anything:
Public Function RecursiveFolder(ByVal path As String) As Long
For Each fold As String In My.Computer.FileSystem.Get
MsgBox(fold)
Next
End
If I were to use the above code on a path to an external hard drive, all subdirectories would be acknowledged. Though if I were to chose "C:\Users\" on Vista I get nothing. So while this code would be most efficient for an XP machine with no preset permissions, it is useless for my requirements.
ASKER
So I was looking at your code there and noticed the following (edited):
For Each di In di_array
' recursive call to this subroutine
Doit(di.FullName)
Next
I don't see much difference in that code from my original:
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
For Each Fil In Fils
MsgBox(Fil.Name)
SearchFolders(Fil.FullName & "\")
Next
End Function
The problem though is that it doesn't appear to be recursive. Am I doing something wrong or do you think I'm encountering an error and it's just stopping on me?
For Each di In di_array
' recursive call to this subroutine
Doit(di.FullName)
Next
I don't see much difference in that code from my original:
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
For Each Fil In Fils
MsgBox(Fil.Name)
SearchFolders(Fil.FullName
Next
End Function
The problem though is that it doesn't appear to be recursive. Am I doing something wrong or do you think I'm encountering an error and it's just stopping on me?
ASKER
I tried adding an error catch and no error messages arose. It appears my code is simply not properly formated for recursion:
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
Dim d_count As Long
For Each Fil In Fils
Try
MsgBox("Dir: " & Fil.FullName)
d_count += 1
Catch ex As Exception
MsgBox("Error: " & ex.Message)
' not fatal
End Try
MsgBox(Fil.Name)
SearchFolders(Fil.FullName )
Next
End Function
Public Function SearchFolders(ByVal path As String) As Long
Dim Dir As New IO.DirectoryInfo(path)
Dim Fils As IO.DirectoryInfo() = Dir.GetDirectories()
Dim Fil As IO.DirectoryInfo
Dim d_count As Long
For Each Fil In Fils
Try
MsgBox("Dir: " & Fil.FullName)
d_count += 1
Catch ex As Exception
MsgBox("Error: " & ex.Message)
' not fatal
End Try
MsgBox(Fil.Name)
SearchFolders(Fil.FullName
Next
End Function
ASKER
Oh no. It does appear to work fine. I tried it on my D:\ drive and it works fine...why can I browse the first few folders in C:\Users\ on Vista, but not the rest? That doesn't really make any sense. It is imperitive that I can scan all user folders. How can I easily incorporate this functionality?
I remember there were no issues with VB6 code, I just don't remember that code (I think it was API calls).
I remember there were no issues with VB6 code, I just don't remember that code (I think it was API calls).
ASKER
So here's a summary, on vista my program will browse C:\Users all the way to "C:\Users\Administrator\Ap pData\Loca l\applicat ion data" (4 folders deep). Now if I type that folder into my explorer it says access denied. Why is it that the program actually sees a folder that I can't manually browse to and more importantly why does it stop there? How do I force the program to continue?
So, what if I was to tell you that the sample that I posted, doesn't suffer from that problem... and that it will continue to list all files and directories even after it encounters an error
Here is an entire application based upon the principles in that example: http://home.hot.rr.com/gra ye/Temp/FS _Dump.zip
ASKER
I would have to say that I thought I tried your variation on the code to no avial. Perhaps I overlooked something? I'll have to check out the application later this evening if possible. Thanks for your help thus far!
ASKER
I downloaded the code and inspected it. I found no differences in my code other than the absence of this call: System.Security.Principal. SecurityId entifier. Is this something I should be including to allow access? I'm sorry if I'm missing something obvious. I've even tried running my program as administrator. It works flawlessly on xp and not at all on Vista =/
ASKER
There has got to be a simple solution to this. What if I simply wanted to create a program that deletes one file in my user directory on Vista in vb.net? Isn't there a simple solution to have the program prompt for rights or something?
Yeah, but did the FS_Dump application work as I described?
If so, then it should be rather easy to discover where your application is different
No, System.Security.Principal. SecurityId entifier has nothing to do with this
If so, then it should be rather easy to discover where your application is different
No, System.Security.Principal.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
So, that just means you'll need to use you're first approach... with a tiny modification to include the "detect and continue" feature.
The following is an example from one of my programs (so ignore the parts that are not relevant)
BTW: The .Net Framework doesn't have a way to traverse a directory path that greater than 260 characters long. If that's also a problem you run accross, I've got replacement set of System.IO classes that will fix that.
Open in new window