Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate TreeView with Directory/File info. 1

Status
Not open for further replies.

cehowski

Technical User
Dec 1, 2006
39
0
0
US
VB.Net 2003/Windows XP

Can someone point me to an example of populating a TreeView / ListView with folder/file info? I'd like to present a DropDown tree view of a directory structure (and files) for a user to use in order to select reports to be viewed in a Crystal Previewer app. Currently I'm hardcoding a Menu for that purpose, but it would be much more flexible if I could just designate a starting point, and show a tree view for the same purpose.

Thanks in Advance.
 
Here is the code that will loop through your directories. I've posted it here before.

The actual purpose of this code is to backup a drive, but I'm sure you can use it for your purposes.

Instaed of the File.Copy command, you would instantiate your treenodes here, and in the Create Directories.

Do a search in this forum and I know you'll find lots of examples.

Code:
Public Sub LoopThruDir(ByVal StartDir As String)
        Dim Tar As String
        Dim S As String
        BU += M + T + Y
        OldDIR = "Backup" + oM + oT + oY
        
        CreateDir(StartDir)
        Dim Files As String() = Directory.GetFiles(StartDir)
        Dim FileName As String
        For Each FileName In Files
            Tar = Mid(FileName, 3, Len(FileName))
            If Tar <> "System Volume Information" Then
                Tar = D + ":\Backup" + M + T + Y + Tar
            Else
                GoTo nFile
            End If

            'MsgBox(Tar)

            Try
               [b] File.Copy(FileName, Tar, True)[/b]
                S = Mid(FileName, 3, Len(FileName))

                WriteLabel("Copying: " & S)
                Refresh()
                i += 1
            Catch
                AL.Add(FileName)
            End Try

            ''Send progress
            Try
                If PB1.Value + 1 > PB1.Maximum Then
                    PB1.Value = 0
                Else
                    PB1.Value = PB1.Value + 1
                End If
            Catch
            End Try
nFile:
        Next FileName
        Dim DirEntries As String() = Directory.GetDirectories(StartDir)
        Dim SubDir As String
        For Each SubDir In DirEntries
            Try
                S = Mid(SubDir, 3, Len(SubDir))
                WriteLabel("Creating: " & S)
                Refresh()
                LoopThruDir(SubDir)
                Refresh()
            Catch

            End Try

        Next SubDir

    End Sub


Public Sub CreateDir(ByVal strDir As String)
        Try
            Dim s As String
            s = Mid(strDir, 3, Len(strDir))
            ''''''''''''''''''here
            s = D + ":\Backup" + M + T + Y & s
            ''''''''''''''''''here
            Directory.CreateDirectory(s)
            Refresh()

        Catch ex As Exception
            WriteInfo(ex.Message)
        End Try
    End Sub

I hope this helps.

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
Wow, thanks for all of the responses. I'll have to take a look at them and see which route looks best.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top