1.

Solve : Sub Directory in VB.net?

Answer»

Hello,

I wrote a program in vb.net that shows shows the path of every file in a directory in a messagebox however I would like to do the same thing so that the program shows every file in the main directory and sub directories. Here is my code:

Dim dirInfo As New System.IO.DirectoryInfo("C:\my documents")
Dim file As System.IO.FileInfo
Dim files() As System.io.FileInfo = dirInfo.GetFiles("*.*")
For Each file In files
Dim sr As New IO.StreamReader(file.FullName)
Dim str As String = sr.ReadToEnd
MESSAGEBOX.SHOW(file.fullname)
Next


Thanks

Al968You need to make your code recursive (a module calling itself) in order to climb down each branch of the directory tree. I found an EXAMPLE of recursion in the snippet closet. It's written in VBScript but it should give you some idea how to go about it.

Code: [Select]Set fso = CREATEOBJECT("Scripting.FileSystemObject")
Set dc = fso.Drives

For each ds in dc
Select Case ds.DriveType
Case 2
Set RootDir = fso.GetFolder(ds & "\") 'starting directory goes here
GetThePaths(RootDir)
End Select
Next

Function GetThePaths(Folder)
For Each Subfolder in Folder.SubFolders
GetTheFiles(Subfolder.Path)
GetThePaths Subfolder
Next
End Function

Function GetTheFiles(FileFolder)
Set f = fso.GetFolder(FileFolder)
Set fc = f.Files
For Each fs in fc
WScript.Echo fs
End If
Next
End Function

Good luck. 8-)Thanks for the responce.
"It's written in VBScript but it should give you some idea how to go about it. "
Actually I have no idea how VBScript works
But thanks anyways

Al968
Quote

Actually I have no idea how VBScript works

VBScript is a subset of VB but without the forms, the variable declarations, and all that pesky compiling. Actually the snippet was posted to show you a method to accomplish your task. It was not posted as a solution as there are differences between VB and VBScript.

8-)Well thanks for your help
I understand your code a little but I don't KNOW how to write it in vb.net :-/
But I least know I have something

Thanks

Al968


Discussion

No Comment Found