Obtain a listing of files for a specified directory and place them in a ListBox.
[C#]
///<summary>Obtain a listing of files for a specified directory and place them in a ListBox.</summary> /// ///<param name="directory">A System.String that is the directory for which you want to list files into a ListBox. Ex: "C:\temp"</param> ///<param name="listbox">A System.Windows.Forms.Listbox control.</param> /// ///<remarks></remarks> public void ListFilesToListBox(System.String directory, System.Windows.Forms.ListBox listbox) { System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(directory); if (directoryInfo != null) { System.IO.FileInfo[] fileInfo = directoryInfo.GetFiles(); System.Int32 i = 0; System.Int32 tempFor1 = fileInfo.GetUpperBound(0); for (i = 0; i <= tempFor1; i++) { listbox.Items.Add(fileInfo[i].FullName); } } }
[Visual Basic .NET]
'''<summary>Obtain a listing of files for a specified directory and place them in a ListBox.</summary> ''' '''<param name="directory">A System.String that is the directory for which you want to list files into a ListBox. Ex: "C:\temp"</param> '''<param name="listbox">A System.Windows.Forms.Listbox control.</param> ''' '''<remarks></remarks> Public Sub ListFilesToListBox(ByVal directory As System.String, ByVal listbox As System.Windows.Forms.ListBox) Dim directoryInfo As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(directory) If Not (directoryInfo Is Nothing) Then Dim fileInfo As System.IO.FileInfo() = directoryInfo.GetFiles Dim i As System.Int32 For i = 0 To fileInfo.GetUpperBound(0) listbox.Items.Add(fileInfo(i).FullName) Next i End If End Sub