Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get all files in Folder and all subfolders

Posted on 2006-10-25
4
513 Views
Last Modified: 2010-04-23
Hello

How can I get all files in a folder and in all subfolders of a folder in a listview?

Thanks

Sven
0
Comment
Question by:Ruttensoft
  • 3
4 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17805551
What version of VB .Net 2003 or 2005?
0
 
LVL 10

Accepted Solution

by:
gangwisch earned 300 total points
ID: 17805764
Private Sub ShowFiles()
  Dim d() As String

  d = System.IO.Directory.GetFiles("c:\")

  Dim en As System.Collections.IEnumerator

  en = d.GetEnumerator

  While en.MoveNext
     Console.WriteLine(CStr(en.Current)) 'en.current will hold the link to your current file
  End While
End Sub
0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 200 total points
ID: 17805913
Hi Ruttensoft;

In VB 2005 you can get a list of all files including subdirectories and place them all into a ListView cvontrol as follows.

        Dim Dir As String = "Directory Path To Search"
        Dim files() As String = Directory.GetFiles(Dir, "*.*", SearchOption.AllDirectories)
        Dim lvi(files.Length - 1) As ListViewItem
        Dim idx As Integer = 0

        For Each file As String In files
            lvi(idx) = New ListViewItem(file)
            idx += 1
        Next
        If lvi.Length > 0 Then
            ListView1.Items.AddRange(lvi)
        End If

Fernando
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 17806327
Hi Ruttensoft;

I want to make sure I did not misunderstand the question. Your question was "How can I get all files in a folder AND IN ALL SUBFOLDERS of a folder in a LISTVIEW?". If that was the question how does the solution that gangwisch provided do that?

My last post was for VB 2005 the below code will work for VB 2003.

    ' Class level variable
    Private fileNames As New ArrayList

To get ALL files

        Dim Dir As String = "Directory path to the Folder"

        GetAllFiles(Dir)

        Dim lvi(fileNames.Count - 1) As ListViewItem
        Dim idx As Integer = 0

        For Each file As String In fileNames
            lvi(idx) = New ListViewItem(file)
            idx += 1
        Next
        If lvi.Length > 0 Then
            ListView1.Items.AddRange(lvi)
        End If



    Private Sub GetAllFiles(ByVal dir As String)

        Dim curInfo As New DirectoryInfo(dir)
        Dim dis() As DirectoryInfo = curInfo.GetDirectories
        Dim files() As FileInfo = curInfo.GetFiles

        For Each fi As FileInfo In files
            fileNames.Add(fi.FullName)
        Next

        For Each di As DirectoryInfo In dis
            GetAllFiles(di.FullName)
        Next

    End Sub


Fernando
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question