searching for files in VB.net

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I want to search for all the mp3's on my hard drive, and put them in a
combo box, so I can click them and they will play in a player.

I figured putting them in an array would work so that I could
correspond the name of the song with an alias of some sort, then have
just have the alias displayed. But I am unsure of how I can set up a
search to find the mp3 files.

any help would be nice

Ryan
 
You can declare a Directoryinfo instance like

Dim di as New DirectoryInfo("C:\Whereeveryouwanttolook")

Dim fi as New FileInfo

for each fi in di.GetFiles
combobox.Items.Add(fi.Fullname)'Or whatever
next
 
Will this work if the files are MP3's? or does it just take the name
of the file no matter what the type?

Also could there be a search done for all MP3's on the system? I ask
this because I want to know if different types of files can be found,
not just '.txt' files. And I do not want to manually type them in

Thanks

Ryan
 
(e-mail address removed) (Ryan) wrote in
Will this work if the files are MP3's? or does it just take the name
of the file no matter what the type?

You'll have to test for the extension:

Dim di as New DirectoryInfo("C:\Whereeveryouwanttolook")

Dim fi as New FileInfo

for each fi in di.GetFiles
If Path.GetExtension(fi.FullName).ToUpper = ".MP3" Then
combobox.Items.Add(fi.Fullname)'Or whatever
End If
next
 
With said:
I want to search for all the mp3's on my hard drive, and put them in a
combo box, so I can click them and they will play in a player.

I used this Code to search for files with an extension of CTB. My "CTBFilPth"
was (in my case) the start folder. For example:

Dim CTBFilPth As String ' CTB Plot Style Files Path
CTBFilPth = "C:\Program Files\AutoCAD LT 2002\Plot Styles\"

So take this and play with it. It will search sub-folders too... When it
finds a CTB file, it adds it to a Combox (cmbCTB):

' Find Plot Styles in Multiple Folders and Lists them
' Get Folder Names Listing in Plot Styles Path
Dim rootDi As New DirectoryInfo(CTBFilPth)
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
Dim CTBdirs() As String = Directory.GetFiles(CTBFilPth & di.Name)
For Each CTBfilname In CTBdirs
CTBtestname = System.IO.Path.GetFileName(CTBfilname)
' Test for CTB files and add to Combox 'cmbCTB'
Dim FilTest As String = CTBtestname.Remove(0, (Len(CTBtestname) - 3))
If UCase(FilTest) = "CTB" Then
cmbCTB.Items.Add(CTBtestname)
End If
Next
Next
I figured putting them in an array would work so that I could
correspond the name of the song with an alias of some sort, then have

You don't have to use an Array... you can select right off the Combox.

Regards,

Bruce
 
Mr. B said:
I used this Code to search for files with an extension of CTB. My "CTBFilPth"
was (in my case) the start folder. For example:

Dim CTBFilPth As String ' CTB Plot Style Files Path
CTBFilPth = "C:\Program Files\AutoCAD LT 2002\Plot Styles\"

So take this and play with it. It will search sub-folders too... When it
finds a CTB file, it adds it to a Combox (cmbCTB):

' Find Plot Styles in Multiple Folders and Lists them
' Get Folder Names Listing in Plot Styles Path
Dim rootDi As New DirectoryInfo(CTBFilPth)
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
Dim CTBdirs() As String = Directory.GetFiles(CTBFilPth & di.Name)
For Each CTBfilname In CTBdirs
CTBtestname = System.IO.Path.GetFileName(CTBfilname)
' Test for CTB files and add to Combox 'cmbCTB'
Dim FilTest As String = CTBtestname.Remove(0, (Len(CTBtestname) - 3))
If UCase(FilTest) = "CTB" Then
cmbCTB.Items.Add(CTBtestname)
End If
Next
Next


You don't have to use an Array... you can select right off the Combox.

Regards,

Bruce

I want to know... is DirectoryInfo a class that I have to create
myself or is it predefined in VB.Net? Everytime that I try and use it,
VB.Net tells me that it needs to be defined. Could someone please
explain this to me

Thank You

Ryan
 
Mr. B said:
I used this Code to search for files with an extension of CTB. My "CTBFilPth"
was (in my case) the start folder. For example:

Dim CTBFilPth As String ' CTB Plot Style Files Path
CTBFilPth = "C:\Program Files\AutoCAD LT 2002\Plot Styles\"

So take this and play with it. It will search sub-folders too... When it
finds a CTB file, it adds it to a Combox (cmbCTB):

' Find Plot Styles in Multiple Folders and Lists them
' Get Folder Names Listing in Plot Styles Path
Dim rootDi As New DirectoryInfo(CTBFilPth)
Dim di As DirectoryInfo
For Each di In rootDi.GetDirectories
Dim CTBdirs() As String = Directory.GetFiles(CTBFilPth & di.Name)
For Each CTBfilname In CTBdirs
CTBtestname = System.IO.Path.GetFileName(CTBfilname)
' Test for CTB files and add to Combox 'cmbCTB'
Dim FilTest As String = CTBtestname.Remove(0, (Len(CTBtestname) - 3))
If UCase(FilTest) = "CTB" Then
cmbCTB.Items.Add(CTBtestname)
End If
Next
Next


You don't have to use an Array... you can select right off the Combox.

Regards,

Bruce

PLease disregard the last message that was posted...I forgot to import
the systems.IO and system name spaces

Ryan
 
With said:
PLease disregard the last message that was posted...I forgot to import
the systems.IO and system name spaces

Oops... sorry about not mentioning it (:

Bruce
 
Back
Top