Home > Enterprise >  List(of String) to Class
List(of String) to Class

Time:06-22

I've been working on it for hours, but I can't find the solution. Probably easy, so a question in the round.

I have a list(of String) with hundreds of directorystring in it, like this:

  "c:\temp\in",
  "c:\temp\in - Kopie",
  "c:\temp\in - Kopie\1",
  "c:\temp\in - Kopie\1\yyy",
  "c:\temp\in - Kopie\1\yyy\yyyyy",
  "c:\temp\in - Kopie\2",
  "c:\temp\in2",
  "c:\temp\ipadb",
  "c:\temp\out",
  "c:\temp\out2",
  "c:\temp\Processes2",
  "c:\temp\Processes2\Kassenbelege",
  "c:\temp\Processes2\Kassenbelege\images",
  "c:\temp\Processes2\Posteingang",
  "c:\temp\Processes2\Posteingang\images",
  "c:\temp\Processes2\Rechnungen",
  "c:\temp\Processes2\Rechnungen\images",
  "c:\temp\Processes2\Rechnungen\images\backup",
  "c:\temp\test"

I have a class that I want to fill from this list.

Public Class _Folder
    Public Property Name As String
    Public ReadOnly Property SubFolders As List(Of _Folder) = New List(Of _Folder)()
End Class

Any help is appreciated. Thank you.

CodePudding user response:

This code should get you there.

  Dim Folders As New List(Of String)({"c:\temp\in",
          "c:\temp\in - Kopie",
          "c:\temp\in - Kopie\1",
          "c:\temp\in - Kopie\1\yyy",
          "c:\temp\in - Kopie\1\yyy\yyyyy",
          "c:\temp\in - Kopie\2",
          "c:\temp\in2",
          "c:\temp\ipadb",
          "c:\temp\out",
          "c:\temp\out2",
          "c:\temp\Processes2",
          "c:\temp\Processes2\Kassenbelege",
          "c:\temp\Processes2\Kassenbelege\images",
          "c:\temp\Processes2\Posteingang",
          "c:\temp\Processes2\Posteingang\images",
          "c:\temp\Processes2\Rechnungen",
          "c:\temp\Processes2\Rechnungen\images",
          "c:\temp\Processes2\Rechnungen\images\backup",
          "c:\temp\test"})
    Dim RootFolder As New _Folder
    For Each f In Folders
        Dim drive = f.Split(":")(0)
        Dim path = f.Split(":")(1)
        Dim pathSplit() As String = path.Split(System.IO.Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)
        Dim currentFolder As _Folder = RootFolder
        For Each level In pathSplit
            Dim foundFolder = (From x In currentFolder.SubFolders Where x.Name = level Select x).FirstOrDefault
            If foundFolder Is Nothing Then
                foundFolder = New _Folder
                foundFolder.Name = level
                currentFolder.SubFolders.Add(foundFolder)
            End If
            currentFolder = foundFolder
        Next
    Next

CodePudding user response:

thank you for your help. This was the final solution. I added some code to make it also usable for network shares and I added the folderpath in the class to acces this information while browsing in the treeview.

Private Sub lblFilter_TextChanged(sender As Object, e As TextChangedEventArgs) Handles lblFilter.TextChanged

    Dim lstFolders As New List(Of _Folder)
    Dim RootFolder As New _Folder

    For Each f In _items
        If f.Contains("\\") Then f = f.Replace("\\", ":")
        Dim drive = f.Split(":")(0)
        Dim path = f.Split(":")(1)

        'If String.IsNullOrEmpty(drive) Then Continue For
        RootFolder.Name = drive

        Dim charSeparators As Char() = New Char() {System.IO.Path.DirectorySeparatorChar}
        Dim pathSplit() As String = path.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)

        Dim currentFolder As _Folder = RootFolder
        For Each level In pathSplit
            If String.IsNullOrEmpty(level) Then Continue For
            Dim foundFolder = (From x In currentFolder.SubFolders Where x.Name = level Select x).FirstOrDefault
            If foundFolder Is Nothing Then
                foundFolder = New _Folder
                foundFolder.Name = level
                If f.StartsWith(":") Then 'Netzwerkpfad
                    foundFolder.FullPath = (f.Substring(0, f.IndexOf(level)) & level).Replace(":", "\\")
                Else 'Pfad mit Laufwerksbuchstaben
                    foundFolder.FullPath = f.Substring(0, f.IndexOf(level)) & level
                End If
                currentFolder.SubFolders.Add(foundFolder)
            End If
            currentFolder = foundFolder
        Next

    Next

    lstFolders.Add(RootFolder)

    tvw.DataContext = lstFolders

End Sub

This class is filled:

Public Class _Folder
    Public Property Name As String
    Public Property FullPath As String
    Public ReadOnly Property SubFolders As List(Of _Folder) = New List(Of _Folder)()
End Class
  • Related