如何将treeview的内容写入文本文件

问题描述 投票:0回答:1

我构建了一个具有不同级别的父节点和子节点的TreeView。

我现在的挑战是我不知道如何将TreeView的内容写入文本文件。我还希望能够读取文本文件以再次重新填充我的TreeView。

我怎么能这样做?

任何想法和/或代码片段将不胜感激。我正在使用Visual Basic 2010 Express。先感谢您。

vb.net treeview
1个回答
1
投票

您可以通过多种方式实现此目的,但我认为最好的方法是将树视图节点作为二进制数据保存到平面文件中,并在需要时将其读回。

这是一个简单的例子。您可以创建一个新的vb.net winforms项目,只需将此代码复制/粘贴到Form1的代码上,然后单击“运行”:

Public Class Form1
    Dim tv As New TreeView
    Dim cmdSave As New Button
    Dim cmdClear As New Button
    Dim cmdLoad As New Button

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Setup control positions and values

        tv.Location = New Point(0, 0)
        tv.Size = New Size(Me.Width, Me.ClientSize.Height - cmdSave.Height)
        cmdSave.Location = New Point(0, Me.ClientSize.Height - cmdSave.Height)
        cmdSave.Text = "Save"
        AddHandler cmdSave.Click, AddressOf cmdSave_Click
        cmdClear.Location = New Point(cmdSave.Left + cmdSave.Width, Me.ClientSize.Height - cmdClear.Height)
        cmdClear.Text = "Clear"
        AddHandler cmdClear.Click, AddressOf cmdClear_Click
        cmdLoad.Location = New Point(cmdClear.Left + cmdClear.Width, Me.ClientSize.Height - cmdLoad.Height)
        cmdLoad.Text = "Load"
        AddHandler cmdLoad.Click, AddressOf cmdLoad_Click

        ' Build the treeview

        tv.Nodes.Add("Node 1")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 1")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 2")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 1 - Child 3")
        tv.Nodes.Add("Node 2")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 1")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 2")
        tv.Nodes(tv.Nodes.Count - 1).Nodes.Add("Node 2 - Child 3")

        tv.ExpandAll()

        ' Add controls to the form

        Me.Controls.Add(tv)
        Me.Controls.Add(cmdSave)
        Me.Controls.Add(cmdClear)
        Me.Controls.Add(cmdLoad)
    End Sub

    Private Sub cmdSave_Click(sender As Object, e As EventArgs)
        Try
            Dim dlg As New SaveFileDialog

            dlg.DefaultExt = "sav"
            dlg.Filter = "sav files|*.sav"
            dlg.OverwritePrompt = True

            If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
                ' Save the treeview nodes to file

                Dim oTreeList As New List(Of clsTreeFile)

                For Each oNode As TreeNode In tv.Nodes
                    Dim oTree As New clsTreeFile

                    oTree.oTreeNode = oNode
                    oTreeList.Add(oTree)
                Next

                Using oFileStream As IO.FileStream = IO.File.Open(dlg.FileName, IO.FileMode.Create)
                    Dim oBinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                    oBinaryFormatter.Serialize(oFileStream, oTreeList)
                End Using

                MessageBox.Show("Treeview saved successfully.", "File saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show("An error occurred while saving:" & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub cmdClear_Click(sender As Object, e As EventArgs)
        tv.Nodes.Clear()
    End Sub

    Private Sub cmdLoad_Click(sender As Object, e As EventArgs)
        Dim dlg As New OpenFileDialog

        Try
            dlg.DefaultExt = "sav"
            dlg.Filter = "SAV files|*.sav"

            If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
                ' Open saved file and read the binary data back to the treeview

                tv.Nodes.Clear()

                Dim oTreeList As New List(Of clsTreeFile)

                Using oFileStream As IO.FileStream = IO.File.Open(dlg.FileName, IO.FileMode.Open)
                    Dim oBinaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                    oTreeList = CType(oBinaryFormatter.Deserialize(oFileStream), List(Of clsTreeFile))
                End Using

                For Each oNode As clsTreeFile In oTreeList
                    tv.Nodes.Add(oNode.oTreeNode)
                Next

                tv.ExpandAll()
            End If
        Catch ex As Exception
            MessageBox.Show("The " & System.IO.Path.GetFileName(dlg.FileName) & " file cannot be opened." & vbCrLf & vbCrLf & ex.Message, "Error opening file", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class

<Serializable()> _
Public Class clsTreeFile
    Public oTreeNode As TreeNode
End Class
© www.soinside.com 2019 - 2024. All rights reserved.