Форум АНТИЧАТ

Форум АНТИЧАТ (https://forum.antichat.xyz/index.php)
-   С/С++, C#, Delphi, .NET, Asm (https://forum.antichat.xyz/forumdisplay.php?f=24)
-   -   [C#] Построение TreeView из текстового файл (https://forum.antichat.xyz/showthread.php?t=163956)

hacknick 16.12.2009 13:06

[C#] Построение TreeView из текстового файл
 
Народ! Очень нужна ваша помощь!

Имеется текстовый файл содержащий пути и размеры ко всем папкам на диске, со следующим синтаксисом:

Код:

c:\Windows\inf\rdyboost;19353
c:\Windows\inf\rdyboost\0000;4284
c:\Windows\inf\rdyboost\0407;5086
c:\Windows\inf\rdyboost\0409;4344
c:\Windows\inf\rdyboost\0419;4734
c:\Windows\inf\RemoteAccess;30958
c:\Windows\inf\RemoteAccess\0000;6918
c:\Windows\inf\RemoteAccess\0407;7748
c:\Windows\inf\RemoteAccess\0409;6918
c:\Windows\inf\RemoteAccess\0419;7554
c:\Windows\inf\ru-RU;750
c:\Windows\inf\ServiceModelEndpoint 3.0.0.0;316650
c:\Windows\inf\ServiceModelEndpoint 3.0.0.0\0000;52
c:\Windows\inf\ServiceModelEndpoint 3.0.0.0\0407;52
c:\Windows\inf\ServiceModelEndpoint 3.0.0.0\0409;52
c:\Windows\inf\ServiceModelEndpoint 3.0.0.0\0419;52
c:\Windows\inf\ServiceModelOperation 3.0.0.0;246004
c:\Windows\inf\ServiceModelOperation 3.0.0.0\0000;53
c:\Windows\inf\ServiceModelOperation 3.0.0.0\0407;53
c:\Windows\inf\ServiceModelOperation 3.0.0.0\0409;53
c:\Windows\inf\ServiceModelOperation 3.0.0.0\0419;53
c:\Windows\inf\ServiceModelService 3.0.0.0;561536
c:\Windows\inf\ServiceModelService 3.0.0.0\0000;51
c:\Windows\inf\ServiceModelService 3.0.0.0\0407;51
c:\Windows\inf\ServiceModelService 3.0.0.0\0409;51
c:\Windows\inf\ServiceModelService 3.0.0.0\0419;51

Нужно по этим данным построить дерево каталогов (элементом TreeView)

Кто нить помогите плиз, два дня мучаюсь(((

Galile0 16.12.2009 13:28

В чем именно состоит проблема?

POS_troi 16.12.2009 13:36

В цикле читай строки из файла и в TreeView создавай элемент

Строка - Элемент, Строка - элемент ............

Но чует мое сердце что тут проблема глубже закопана, так что четкое ТЗ в студию.

Ra$cal 16.12.2009 13:49

читаешь строку, сплитишь ее по '\\', делаешь метод, который обходит TreeView в поисках элемента с именем таким то(которое просплитилось), и так идешь, пока в исходной строке не остается конечный узел. ТОогда его и добавляешь. А вообще тут пахнет xml. Разбор строки в xml и отображение xml в treeview.

hacknick 16.12.2009 16:18

Решил проблему))
Задача была имитация жесткого диска )))
Тему можно закрывать

Если кому надо:
Сделал на вб, если кому надо на с шарп - переписать дело 5 минут ;)
Код:

Imports System.IO

Public Class Form1
    Dim tfName, TreecDir, TreecFn, TreecFnType, TreecSp, TreecDsinb, lgName, stupName As String
    Dim Treeci As Integer
    Dim Treecds As IO.DirectoryInfo


    Private Sub LoadTreeViewFromFile(ByVal file_name As String, _
    ByVal trv As TreeView)
        ' Get the file's contents.
        Dim stream_reader As New StreamReader(file_name)
        Dim file_contents As String = stream_reader.ReadToEnd()
        stream_reader.Close()

        ' Remove line feeds.
        file_contents = file_contents.Replace(vbLf, "")

        ' Break the file into lines.
        Const charCR As Char = CChar(vbCr)
        Const charTab As Char = CChar(vbTab)
        Dim lines() As String = file_contents.Split(charCR)

        ' Process the lines.
        Dim text_line As String
        Dim level As Integer
        Dim tree_nodes() As TreeNode
        Dim num_nodes As Integer = 0
        ReDim tree_nodes(num_nodes)

        trv.Nodes.Clear()
        For i As Integer = 0 To lines.GetUpperBound(0)
            text_line = lines(i)
            If text_line.Trim().Length > 0 Then
                ' See how many tabs are at the start of the
                ' line.
                level = text_line.Length - _
                    text_line.TrimStart(charTab).Length

                ' Make room for the new node.
                If level > num_nodes Then
                    num_nodes = level
                    ReDim Preserve tree_nodes(num_nodes)
                End If

                ' Add the new node.
                If level = 0 Then
                    tree_nodes(level) = trv.Nodes.Add(text_line.Trim())
                Else
                    tree_nodes(level) = tree_nodes(level - 1).Nodes.Add(text_line.Trim())
                End If
                tree_nodes(level).EnsureVisible()
            End If
        Next i

        If trv.Nodes.Count > 0 Then trv.Nodes(0).EnsureVisible()
    End Sub

    Private Sub gettin(ByVal startpath As String)
        Try
            For Each d As IO.DirectoryInfo In New IO.DirectoryInfo(startpath).GetDirectories
                Treecds = New IO.DirectoryInfo(d.FullName)
                TreecSp = Space(Treeci)
                TreecSp = TreecSp.Replace(" ", vbTab)
                TreecDsinb = TreecSp & d.Name
                TreecFn = TreecDsinb
                Using sw As New IO.StreamWriter(tfName, True)
                    sw.WriteLine(TreecFn)
                    sw.Close()
                End Using
                Try : If CBool(d.GetDirectories.Length) Then Treeci += 1 : gettin(d.FullName) : Treeci -= 1
                Catch ex As Exception
                End Try
            Next
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tfName = "c:\tr"
        'gettin("c:\")
        LoadTreeViewFromFile(tfName, TreeView1)
        -
    End Sub
End Class



Время: 10:22