-
Jul 9th, 2020, 05:35 PM
#1
Thread Starter
Addicted Member
[SOLVED] GetFiles - Multiple File Ext Patterns
Hey all,
I am using the below code to fill a listbox with just exensions I want.
If I just do "*.jpg" by itself it works fine. Loads all files with that extension
However, I am trying to add multiple extensions patterns, but my list box goes blank.
Code:
Dim filesI As New List(Of String)
Dim fileStrI
fileStrI = txtIMG.Text & "\"
filesI.AddRange(IO.Directory.GetFiles(fileStrI, "*.jpg,*.png"). '<--- DOESN'T WORK AT RUNTIME - NO ERRORS AT DESIGNTIME
Select(Function(f) IO.Path.GetFileName(f)))
ListBox3.Items.Clear()
ListBox3.Items.AddRange(filesI.ToArray)
I know I don't have this right.
I have been doing a lot of digging on Google and many offer sample code, but none if them seem to work.
Can any one help me with this??
Thanks in advance
Last edited by pixelink; Jul 10th, 2020 at 05:12 AM.
-
Jul 9th, 2020, 05:59 PM
#2
Re: GetFiles - Mutiple File Ext Patterns
Well of course it does not work as the documentation states https://docs.microsoft.com/en-us/dot...System_String_
Code:
dir.getfiles.where(function(fi) fi.extension = ".png" orelse fi.extension = ".jpeg").toarray
If you are using LINQ EnumerateFiles seems more fitting.
-
Jul 9th, 2020, 07:56 PM
#3
Re: GetFiles - Mutiple File Ext Patterns
vb.net Code:
Private Function GetFiles(folderPath As String, ParamArray patterns As String()) As String()
Dim filePaths As IEnumerable(Of String) = New String() {}
For Each pattern In patterns
filePaths = filePaths.Concat(Directory.EnumerateFiles(folderPath, pattern))
Next
Return filePaths.ToArray()
End Function
Sample usage:
vb.net Code:
Dim filePaths = GetFiles(folderPath, "*.jpg", "*.png")
That said, your code could do with some improving. I would tend to use DirectoryInfo and FileInfo and bind to the ListBox. That way, you can display the file name but still access the full path via the SelectedValue.
vb.net Code:
Private Function GetFileInfos(folderPath As String, ParamArray patterns As String()) As FileInfo()
Dim folder As New DirectoryInfo(folderPath)
Dim files As IEnumerable(Of FileInfo) = New FileInfo() {}
For Each pattern In patterns
files = files.Concat(folder.EnumerateFiles(pattern))
Next
Return files.ToArray()
End Function
Sample usage:
vb.net Code:
With ListBox3
.DisplayMember = "Name"
.ValueMember = "FullName"
.DataSource = GetFileInfos(folderPath, "*.jpg", "*.png")
End With
You can replace all your code with that. When the user selects a file by name from the list, you can get the full path of the file from the SelectedValue of the ListBox.
-
Jul 10th, 2020, 04:50 AM
#4
Thread Starter
Addicted Member
Re: GetFiles - Mutiple File Ext Patterns
@jmc
The second suggestion works fine.
Thanks for your help
Last edited by pixelink; Jul 10th, 2020 at 05:05 AM.
-
Jul 10th, 2020, 04:57 AM
#5
Re: [SOLVED] GetFiles - Mutiple File Ext Patterns
Then I can only conclude that you must have no JPG or PNG files in the folder you specified. I just tested that exact code and it worked just as expected.
-
Jul 10th, 2020, 05:04 AM
#6
Thread Starter
Addicted Member
Re: [SOLVED] GetFiles - Mutiple File Ext Patterns
Oops... it loads the appropriate extensions fine.
However, when i select a file name from list box it generate an error.
"Conversion from type 'FileInfo' to type 'String' is not valid."
Code for selecting image file to display in picurebox
Code:
If ListBox3.Items.Count = 0 Then Exit Sub
pbIMG.Image = Nothing
'Dim img = System.Drawing.Image.FromFile(txtIMG.Text & "\" & ListBox3.SelectedItem)
Dim img = System.Drawing.Image.FromFile(ListBox3.SelectedItem)
pbIMG.Image = img
I need a string to load image, not fileinfo
How do I convert that?
Last edited by pixelink; Jul 10th, 2020 at 05:17 AM.
-
Jul 10th, 2020, 05:06 AM
#7
Thread Starter
Addicted Member
Re: [SOLVED] GetFiles - Mutiple File Ext Patterns
It does load... I just forgot to remove a line in the block.
But, now the image doesn't load in picture box... see post just above
-
Jul 10th, 2020, 05:11 AM
#8
Thread Starter
Addicted Member
Re: [SOLVED] GetFiles - Mutiple File Ext Patterns
Oh, I got it. Forgot to switch "ListBox3.SelectedItem" for "ListBox3.SelectedValue"
final listbox code that works:
Code:
If ListBox3.Items.Count = 0 Then Exit Sub
pbIMG.Image = Nothing
Dim img = System.Drawing.Image.FromFile(ListBox3.SelectedValue)
Thanks @JMC again!!
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
|