|
-
Apr 5th, 2008, 11:53 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2008] Getting a list of files with certain file types
Hi!
How do I get a list of files with a certain extension in a directory and return that list in a combobox?
e.g
Say I want to find all the text files in the "C:\" directory and return their names in a combobox, how would I go about doing it?
Any help is much appreciated.
Louix.
-
Apr 5th, 2008, 02:23 PM
#2
Re: [2008] Getting a list of files with certain file types
Code:
Dim di As New System.IO.DirectoryInfo("C:\")
Dim fi() As System.IO.FileInfo
fi = di.GetFiles("*.txt")
For ct As Integer = 0 To fi.Length - 1
Debug.WriteLine(fi(ct).FullName)
Next
-
Apr 5th, 2008, 02:25 PM
#3
Re: [2008] Getting a list of files with certain file types
Use the GetFiles function.
Code:
Option Explicit On
Option Strict On
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xlFiles() As String
xlFiles = System.IO.Directory.GetFiles("C:\Users\UserName\Documents", "*.xls", IO.SearchOption.TopDirectoryOnly)
ComboBox1.Items.AddRange(xlFiles)
End Sub
End Class
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 5th, 2008, 07:09 PM
#4
Re: [2008] Getting a list of files with certain file types
If you want only the file names, rather than the full paths, displayed in the ComboBox you'd modify what's already been posted:
vb.net Code:
myComboBox.DisplayMember = "Name" myComboBox.ValueMember = "FullName" myComboBox.DataSource = (New IO.DirectoryInfo("folder path here")).GetFiles("*.txt")
By binding an array of FileInfo objects to the control you have access to the file name only as well as the full path. Setting the DisplayMember to "Name" will display just the file names in the control, while setting the ValueMember to "FullName" will return the full path of the selected file from the control's SelectedValue property.
-
Apr 6th, 2008, 09:59 AM
#5
Thread Starter
Addicted Member
Problem Now Solved! Thanks a lot guys! :D
Thanks sooooooooooo much guys you solved my problem and it now works exactly how I wanted it to!
Again, thanks so much!!!!!! 
Louix.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|