visual basic 2008 treeview节点导出到文本文件

时间:2018-04-01 15:15:10

标签: visual-studio

Private Sub btnCreateTreeData(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreateTreeData.Click
    'create buffer for storing string data
    Dim buffer As New System.Text.StringBuilder
    'loop through each of the treeview's root nodes
    For Each rootNode As TreeNode In yourTreeView.Nodes
        'call recursive function
        BuildTreeString(rootNode, buffer)
    Next
    'write data to file
    IO.File.WriteAllText("C:\treeTest.txt", buffer.ToString)
End Sub

文件创建成功但没有树节点

2 个答案:

答案 0 :(得分:0)

在这里我成功获得了树木点头

 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        For Each nd As TreeNode In TreeView1.Nodes
            If nd.Nodes.Count > 0 Then 'it has children, lets look at them

                For Each ndChild As TreeNode In nd.Nodes
                    If ndChild.Nodes.Count > 0 Then 'it has children, lets look at them

                        Dim outputText As String = String.Concat(ndChild.Text, " ", ndChild.Nodes.Count)

                        For Each ndSubChild As TreeNode In ndChild.Nodes
                            outputText = String.Concat(outputText, " ", ndSubChild.Text)

                            TextBox4.Text += vbTab & outputText & vbNewLine

                        Next
                        Debug.Print(outputText)

                        'TextBox4.Text += vbTab & vbTab & vbTab & vbTab & outputText

                    End If
                Next
            End If
        Next
    End Sub

答案 1 :(得分:0)

我可以将其另存为文本文件:

Dim Save As New SaveFileDialog()
    Dim myStreamWriter As System.IO.StreamWriter
    Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
    Save.CheckPathExists = True
    Save.Title = "Save File"
    Save.FileName = My.Computer.FileSystem.SpecialDirectories.Desktop & "\Treee Data"
    Save.ShowDialog(Me)
    Try
        myStreamWriter = System.IO.File.AppendText(Save.FileName)
        myStreamWriter.Write(TextBox4.Text)
        myStreamWriter.Flush()
    Catch ex As Exception
    End Try

    Dim ProcessProperties As New ProcessStartInfo
    ProcessProperties.FileName = "notepad"
    ProcessProperties.Arguments = Save.FileName
    ProcessProperties.WindowStyle = ProcessWindowStyle.Maximized
    Dim myProcess As Process = Process.Start(ProcessProperties)