在树视图中构建远程机器(LAN)的目录结构

时间:2012-10-02 02:10:13

标签: vb.net treeview

我正在构建一个树视图结构,以在我的本地计算机上显示远程计算机的目录。 它适用于在本地计算机上显示目录,但不适用于远程计算机。

以下是代码,如果有人能告诉我如何使用远程计算机完成这项工作,我会很高兴

Imports System.IO
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Linq
Imports System.Drawing
Imports System.Data

Public Class GPSTestAuto
Private mRootPath() As String = Directory.GetFileSystemEntries("\\192.168.0.35\Test Drive\ULTS\")
    Property RootPath As String
        Get
            Return mRootPath(0)
        End Get
        Set(value As String)
            mRootPath(0) = value
        End Set
    End Property
    Private Sub GPSTestAuto_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Dim mRootNode As New TreeNode
        mRootNode.Text = RootPath
        mRootNode.Tag = RootPath

        mRootNode.Nodes.Add("*DUMMY*")
        TreeView1.Nodes.Add(mRootNode)

    End Sub
    Private Sub TreeView1_BeforeCollapse(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
        ' clear the node that is being collapsed
        e.Node.Nodes.Clear()
        ' add a dummy TreeNode to the node being collapsed so it is expandable
        e.Node.Nodes.Add("*DUMMY*")
    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        ' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
        e.Node.Nodes.Clear()
        ' get the directory representing this node
        Dim mNodeDirectory As IO.DirectoryInfo
        mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
        ' add each subdirectory from the file system to the expanding node as a child node
        For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
            ' declare a child TreeNode for the next subdirectory
            Dim mDirectoryNode As New TreeNode
            ' store the full path to this directory in the child TreeNode's Tag property
            mDirectoryNode.Tag = mDirectory.FullName
            ' set the child TreeNodes's display text
            mDirectoryNode.Text = mDirectory.Name
            ' add a dummy TreeNode to this child TreeNode to make it expandable
            mDirectoryNode.Nodes.Add("*DUMMY*")
            ' add this child TreeNode to the expanding TreeNode
            e.Node.Nodes.Add(mDirectoryNode)
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

我认为您的问题与前一段时间的mine有关,并且是由于安全限制。您需要在远程计算机上创建相同的用户帐户,如果代码在Web应用程序中,则必须将其包装到使用本地帐户的WindowsImpersonationContext中。我得到了它的工作,但它比它需要的更复杂。

this question的答案对我来说是最有帮助的。