Click here to Skip to main content
15,901,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking for a solution that will add all files existing on a user computer to a listbox. If the user selects the C:\ I would like the listbox to add all folders and files to the listbox as quickly as possible. No other action is necessary at this point
Posted
Comments
Dale 2012 3-Dec-10 19:05pm    
subdirectories also if possible
Dale 2012 3-Dec-10 19:10pm    
To restate my question I would like the user to select a dir from a dialog box which is done and then once the dir is selected the files from that directory and subdirectories are added to a listbox.

1 solution

If you're using VB.NET, take a look at the DirectoryInfo and FileSystemInfo classes. You can use those to get all the files and then add them to your ListBox. Try Google for examples of iterating through file and directory lists.
 
Share this answer
 
Comments
Dale 2012 3-Dec-10 19:44pm    
Ok I have Found This code which works great!! but it errors when a file is access restricted.... Is there any way to add a catch statement to pass the file to carry on the to next?


Here is the code I have so far:

Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnList As System.Windows.Forms.Button
Friend WithEvents txtDir As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents cboPattern As System.Windows.Forms.ComboBox
Friend WithEvents lstFiles As System.Windows.Forms.ListBox
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
Me.btnList = New System.Windows.Forms.Button
Me.txtDir = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.cboPattern = New System.Windows.Forms.ComboBox
Me.lstFiles = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'btnList
'
Me.btnList.Anchor = System.Windows.Forms.AnchorStyles.Top
Me.btnList.Location = New System.Drawing.Point(290, 64)
Me.btnList.Name = "btnList"
Me.btnList.Size = New System.Drawing.Size(48, 24)
Me.btnList.TabIndex = 2
Me.btnList.Text = "List"
'
'txtDir
'
Me.txtDir.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDir.Location = New System.Drawing.Point(48, 8)
Me.txtDir.Name = "txtDir"
Me.txtDir.Size = New System.Drawing.Size(582, 20)
Me.txtDir.TabIndex = 0
Me.txtDir.Text = ""
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(0, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(50, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Directory"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(0, 32)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(41, 16)
Me.Label2.TabIndex = 3
Me.Label2.Text = "Pattern"
'
'cboPattern
'
Me.cboPattern.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.cboPattern.Items.AddRange(New Object() {"*.vb (Code Files)", "*.resx (Resource Files)", "*.sln (Solution Files)", "*.* (All Files)"})
Me.cboPattern.Location = New System.Drawing.Point(48, 32)
Me.cboPattern.Name = "cboPattern"
Me.cboPattern.Size = New System.Drawing.Size(582, 21)
Me.cboPattern.TabIndex = 1
Me.cboPattern.Text = "*.* (All Files)"
Dr.Walt Fair, PE 3-Dec-10 21:11pm    
Of course you can put a try/catch/finally block around part of the code to catch problems. Personally I prefer to check for problems before they cause an error.

However that looks to me to be mostly designer generated code and I don't see where it actually gets any files or directories.
Dale 2012 3-Dec-10 23:32pm    
ok I will post a new question with the code cleaned up for everyone to look at
Dale 2012 3-Dec-10 23:33pm    
Imports System.IO

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnList As System.Windows.Forms.Button
Friend WithEvents txtDir As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents cboPattern As System.Windows.Forms.ComboBox
Friend WithEvents lstFiles As System.Windows.Forms.ListBox
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
Me.btnList = New System.Windows.Forms.Button
Me.txtDir = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.cboPattern = New System.Windows.Forms.ComboBox
Me.lstFiles = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'btnList
'
Me.btnList.Anchor = System.Windows.Forms.AnchorStyles.Top
Me.btnList.Location = New System.Drawing.Point(290, 64)
Me.btnList.Name = "btnList"
Me.btnList.Size = New System.Drawing.Size(48, 24)
Me.btnList.TabIndex = 2
Me.btnList.Text = "List"
'
'txtDir
'
Me.txtDir.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDir.Location = New System.Drawing.Point(48, 8)
Me.txtDir.Name = "txtDir"
Me.txtDir.Size = New System.Drawing.Size(582, 20)
Me.txtDir.TabIndex = 0
Me.txtDir.Text = ""
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(0, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(50, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Directory"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(0, 32)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(41, 16)
Me.Label2.TabIndex = 3
Me.Label2.Text = "Pattern"
'
'cboPattern
'
Me.cboPattern.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.cboPattern.Items.AddRange(New Object() {"*.vb (Code Files)", "*.resx (Resource Files)", "*.sln (Solution Files)", "*.* (All Files)"})
Me.cboPattern.Location = New System.Drawing.Point(48, 32)
Me.cboPattern.Name = "cboPattern"
Me.cboPattern.Size = New System.Drawing.Size(582, 21)
Me.cboPattern.TabIndex = 1
Me.cboPattern.Text = "*.* (All Files)"
'
'lstFiles
'
Me.lstFiles.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.L
Dr.Walt Fair, PE 3-Dec-10 23:40pm    
That still is just the form layout code. Where do you actually attempt to read the files and directories?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900