none
GetFile Problem RRS feed

  • Question

  • I am atemting to use the "Directory.GetFiles" statement with the SelectedItem or Text from SubCategoryComboBox included.  

    Pleas help me by rectifying the nesasiry line.  

    Private Sub SubCategoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles SubCategoryComboBox.SelectedIndexChanged
    
            TitleListBox.Items.Clear()
            Select Case SubCategoryComboBox.Text
                'Smaaklik Tuisgemaakte Drankies
                Case Is = "Koue Drankies"
    
                Case Is = "Melk Drankies"
    
                Case Is = "Warm Drankies"
    
                    'Hoofgeregte
                Case Is = "Eiergeregte"
    
                Case Is = "Heerlike Sop Resepte"
                    Me.TitleListBox.Items.Clear()
                    Dim Files() As String = Directory.GetFiles(HoofgeregteFolder, Me.SubCategoryComboBox.Text.ToString)
                    For Each file As String In Files
                        TitleListBox.Items.Add(Path.GetFileNameWithoutExtension(file))
                    Next
    

    Saturday, February 22, 2020 11:03 AM

All replies

  • Unsure what the issue is but the second parameter to GetFiles in a search pattern e.g.

    ' Only get files that begin with the letter "c".
    Dim dirs As String() = Directory.GetFiles("c:\", "c*")

    There are three examples, all do the same. Since you are using VS2013 start from the last one as the first two may not work with VS2013.

    '
    ' Get all text files beginning with aa1 and set the to
    ' the data source of a list box. In this case the path
    ' is the app folder but can be any folder you want.
    '
    ListBox1.DataSource = Directory.GetFiles(
        AppDomain.CurrentDomain.BaseDirectory, "aa1*.txt").
        Select(Function(file) Path.GetFileNameWithoutExtension(file)).ToList()

    Or

    Dim results = Directory.GetFiles(
        AppDomain.CurrentDomain.BaseDirectory, "AA1*.txt").
            Select(Function(file) Path.GetFileNameWithoutExtension(file)).ToList()
    
    ListBox1.Items.Clear()
    
    For Each item As String In results
        ListBox1.Items.Add(item)
    Next

    Or

    Dim results = Directory.GetFiles(
        AppDomain.CurrentDomain.BaseDirectory, "AA1*.txt").ToArray()
    
    ListBox1.Items.Clear()
    
    For Each item As String In results
        ListBox1.Items.Add(Path.GetFileNameWithoutExtension(item))
    Next


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, February 22, 2020 12:05 PM
    Moderator
  • Its not clear what your question is. Try to make a smaller question. Just one line of code maybe that you don't understand. We cant know what you want to do. You need to show what you have and what you want.

    There are several lines of your example that are not correct. Maybe this will help for a working example of what you want?

    Public Class Form8
        Private Sub Form8_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.Items.Add("Item 1")
            ComboBox1.Items.Add("Item 2")
            ComboBox1.Items.Add("rusty")
            ComboBox1.SelectedIndex = 0
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            'use the sender to avoid late binding 
            Dim cbox As ComboBox = DirectCast(sender, ComboBox)
            Dim itemName As String = cbox.Items(cbox.SelectedIndex).ToString
    
            Select Case itemName
    
                Case "Item 1"
                    Label1.Text = "Item 1"
                Case "Item 2"
                    Label1.Text = "Item 2"
    
                Case "rusty"
                    Label1.Text = "rusty"
    
                    Dim Files() As String = IO.Directory.GetFiles("c:\bitmaps\", Label1.Text & "*.*")
                    For Each file As String In Files
                        Label1.Text &= vbLf & (IO.Path.GetFileNameWithoutExtension(file))
                    Next
            End Select
    
        End Sub
    End Class

    • Proposed as answer by simonb549 Monday, February 24, 2020 9:02 AM
    Saturday, February 22, 2020 12:16 PM