folders filteren

Status
Niet open voor verdere reacties.

bonnowagt

Gebruiker
Lid geworden
7 dec 2006
Berichten
445
Code:
  Dim FolderBrowserDialog1 As New FolderBrowserDialog
        With FolderBrowserDialog1
           Desktop is the root folder in the dialog.
            .RootFolder = Environment.SpecialFolder.Desktop
          
            .SelectedPath = "c:\windows"
                      .Description = "Select the source directory"
            If .ShowDialog = DialogResult.OK Then
              Display the selected folder if the user clicked on the OK button.
               MessageBox.Show(.SelectedPath)
            End If
        End With

       Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
        For Each filename As String In files
       path As String = System.IO.Path.GetFullPath(filename)
       If System.IO.Directory.Exists(path) Then
      for Each item As String In System.IO.Directory.GetFiles(Path)
           If Not ListBox2.Items.Contains(item) Then If item.EndsWith(".mp3") Then ListBox2.Items.Add(item)
          If Not ListBox2.Items.Contains(item) Then If item.EndsWith(".mp4") Then ListBox2.Items.Add(item)
      If Not ListBox2.Items.Contains(item) Then If item.EndsWith(".flac") Then ListBox2.Items.Add(item)
    Next
      Else
      If Not ListBox2.Items.Contains(Path) Then ListBox2.Items.Add(Path)
     End If
    Next

Ik heb dus een button met het aanroepen van een folder hetgeen ook gebeurd. Vervolgens klik ik de folder aan en wil filteren op muziekfiles .

In de code zit een fout en namelijk onderstaande:

Code:
  Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

De fout is dus e.date
 
Laatst bewerkt door een moderator:
Ik heb inmiddels de oplossing gevonden/ Met 1 uitzondering. Files die eindigen op mp3, wav enz. mogen alleen in listbox1 terecht komen.

De code :
Code:
 Dim FolderBrowserDialog1 As New FolderBrowserDialog
        With FolderBrowserDialog1
            .RootFolder = Environment.SpecialFolder.Desktop
            .SelectedPath = "c:\windows"
            .Description = "Select the source directory"
            If .ShowDialog = DialogResult.OK Then
                Dim FileDirectory As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
                Dim FilesFromDirectory As IO.FileInfo() = FileDirectory.GetFiles()
                For Each File As IO.FileInfo In FilesFromDirectory
       ListBox1.Items.Add(File.FullName)
                Next
            End If
        End With

Dus door ListBox1.Items.Add(File.FullName)
komen alle files van de betreffende folder in de listbox. Ik moet dus alleen de mp3 enz bestanden.
 
Laatst bewerkt door een moderator:
Probeer deze functie met bijbehorende enum:

Code:
    Enum FileMethod
        ''' <summary>Returns the full path.</summary>
        FullPath
        ''' <summary>Returns the directory.</summary>
        Directory
        ''' <summary>Returns the filename with extension.</summary>
        FileNameWithExtension
        ''' <summary>Returns the filename without extension.</summary>
        FileNameWithoutExtension
        ''' <summary>Returns the file extension.</summary>
        FileNameExtension
    End Enum
    ''' <summary>A function that returns files that match the specified filters.</summary>
    ''' <param name="sPath">The path to get the files from.></param>
    ''' <param name="Method">What to get from the specified path.></param>
    ''' <param name="sFilters">The filters to use. Syntax is "*.*", "*.*", "*.*", etc.</param>
    Private Function GetFiles(ByVal sPath As String, ByVal Method As FileMethod, ByVal ParamArray sFilters() As String) As String()

        Dim strList As New List(Of String)

        For Each Filter As String In sFilters
            For Each File In Directory.GetFiles(sPath, Filter)
                If Not strList.Contains(File) Then
                    Select Case Method
                        Case FileMethod.FullPath
                            strList.Add(File)
                        Case FileMethod.Directory
                            strList.Add(IO.Path.GetDirectoryName(File))
                        Case FileMethod.FileNameWithExtension
                            strList.Add(IO.Path.GetFileName(File))
                        Case FileMethod.FileNameWithoutExtension
                            strList.Add(IO.Path.GetFileNameWithoutExtension(File))
                        Case FileMethod.FileNameExtension
                            strList.Add(IO.Path.GetExtension(File))
                    End Select
                End If
            Next
        Next
        Return strList.ToArray

    End Function

Mogelijk moet je System.Runtime.InteropServices importeren om de code documentatie te laten werken.

Aanroepen doe je zo:

Code:
ListBox1.Items.Add("pad hier", method hier, filter hier)
 
Laatst bewerkt:
Bedankt voor de reactie maar kwam er niet helemaal uit. Kwam ineens op een andere idee en heb het op de volgende wijze opgelost.

Ik selecteer een folder en laat vervolgens gewoon alle bestanden door die in een not visible listbox2 worden gezet.

Code:
   For Each item In ListBox2.Items
            If item.EndsWith(".mp3") Then ListBox1.Items.Add(item)
            If item.EndsWith(".flac")  Then ListBox1.Items.Add(item)
            If item.EndsWith(".wav") Then ListBox1.Items.Add(item)
            If item.EndsWith(".mp4") Then ListBox1.Items.Add(item)

        Next

En het werkt...

Bedankt voor alle moeite
 
Prima, zet je de vraag op Opgelost? :)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan