3
Contributors
9
Replies
12
Views
6 Years
Discussion Span
Last Post by horserider
0

I hope it helps :

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' make a reference to a directory
    Dim di As New IO.DirectoryInfo("c:\")
    Dim diar1 As IO.FileInfo() = di.GetFiles()
    Dim dra As IO.FileInfo

   'list the names of all files in the specified directory
    For Each dra In diar1
        ListBox1.Items.Add(dra)
    Next
End Sub
0

Since you have not mentioned which control to add the Files to, I will also use a ListBox.

Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                            (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                'ListBox1.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add only File Name with extension.
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))

To only search the Top Folder and not the SubFolders as well, change FileIO.SearchOption.SearchAllSubDirectories to: FileIO.SearchOption.SearchTopLevelOnly

Edited by __avd: EDIT : Put closing parenthesis at line # 11.

0

Since you have not mentioned which control to add the Files to, I will also use a ListBox.

Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                            (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                'ListBox1.Items.Add(IO.Path.GetFileName(myCoolFile)) '// add only File Name with extension.
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###")

To only search the Top Folder and not the SubFolders as well, change FileIO.SearchOption.SearchAllSubDirectories to: FileIO.SearchOption.SearchTopLevelOnly

But it doesn't work when the Directory has many Subdirectory.System/Program gets hanged. Is ther any soulution for this.

1

See if this helps.
1 Button, 1 ListBox, 1 BackgroundWorker

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync() '// Start BackgroundWorker.
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ListBox1.Items.Clear() '// clear ListBox.
        Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                             (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))
    End Sub
End Class
Votes + Comments
Helpful!
0

See if this helps.
1 Button, 1 ListBox, 1 BackgroundWorker

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync() '// Start BackgroundWorker.
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ListBox1.Items.Clear() '// clear ListBox.
        Dim myCoolFolder As String = "C:\Program Files" '// your Folder.
        Try
            For Each myCoolFile As String In My.Computer.FileSystem.GetFiles _
                                             (myCoolFolder, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
                ListBox1.Items.Add(myCoolFile) '// add File with FullPath.
            Next
        Catch ex As UnauthorizedAccessException
            MsgBox(ex.Message)
        End Try
        MsgBox("Total Files: " & CInt(ListBox1.Items.Count).ToString("#,###,###"))
    End Sub
End Class

Thanks for ur help. But ther is one error in ListBox1.
Error:
Cross-thread operation not valid: Control 'ListBox1' accessed from a thread other than the thread it was created on

How can i solve this error

0

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub
0

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

Thank u very much. It works fine . I had forgot to copy that line.

0

Even w/the code I posted to "allow cross threading"?

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False '// allow cross threading.
    End Sub

One more Help Sir..
how can i get all folders including subfolders.but i do not want list files.

0

One more Help Sir..
how can i get all folders including subfolders.but i do not want list files.

I got it...

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.