简体   繁体   中英

open/import multiple files vb.net

ok im going to try to explain this the best i can.

So i have a list viewer for image thumbnails. I need to be able to click a button and it load the full dir. The issue is the normal way loads the images in but they are out of order. 1.jpg 10.jpg 100.jpg

        For Each file As String In My.Computer.FileSystem.GetFiles(appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\")
        ImageListView1.Items.Add(file)
    Next

So i went and looked around for a filter

        Dim files = Directory.EnumerateFiles(appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\").
                     Select(Function(s) Path.GetFileName(s)).ToList
    Console.WriteLine("Before: {0}", String.Join(", ", files))

    ' sort the list using the Natural Comparer:
    files.Sort(myComparer)
MsgBox((String.Join(", ", files)))

So this script puts them in the right order 1,2,3,4,5.. but i cant figure out how to open it this way. Cause

ImageListViewer1.items.addrange((String.Join(", ", files)))

overloads. Like openFileDialog.FileNames has the ability to open multiple files at once so I know its possible but i don't want to use a dialog. To the point i need a way to load the file string produced by this which is 1.jpg,2.jpg,3.jpg into the ImageViewerList.

This creates a string of the file in the correct order "(String.Join(", ", files))" is there a way i can load files from a string.

For Each file As String In My.Computer.FileSystem.GetFiles(appPath, string

and it be able to load the string of files it creates the string like so 1.jpg,2.jpg,3.jpg continued for all files in the dir it looked at

iv looked around google for assistance and looked at the ms page on getfiles but i have had no luck. any ways any help would be greatly appreciated thanks in advance -Fox

You need to write your own sort function to sort the numeric values. I referred the link , for CustomSort function. Below code sorts the file names and works nicely for me.

Dim Dir As String = appPath + "\" + ConfigurationManager.AppSettings("activedisplay").ToString + "\" + Bfolder.Text + "\"
Dim fileList = New DirectoryInfo(Dir).GetFiles("*.jpg").[Select](Function(o) o.Name).ToList()
Dim sortedList = CustomSort(fileList).ToList()


Public Shared Function CustomSort(list As IEnumerable(Of String)) As IEnumerable(Of String)
    Dim maxLen As Integer = list.[Select](Function(s) s.Length).Max()

    Return list.[Select](Function(s) New With { _
        Key .OrgStr = s, _
        Key .SortStr = Regex.Replace(s, "(\d+)|(\D+)", Function(m) m.Value.PadLeft(maxLen, If(Char.IsDigit(m.Value(0)), " "c, "?"c))) _
    }).OrderBy(Function(x) x.SortStr).[Select](Function(x) x.OrgStr)
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM