Skip to main content
 首页 » 编程设计

c#之TreeView 全路径剥离

2024年02月23日26yxwkf

这是 codeplex 上的应用程序,我所做的就是创建一个新的文本框,并尝试将当前节点的路径选择到这个文本框中,但是我得到了我根本不需要的额外东西,

链接到应用程序,

Codeplex app

我使用的代码行是,

TextBox1.Text = nodeCurrent.FullPath; 

我得到的输出是这样的,

My Computer\C:\\Documents and Settings\Administrator\Desktop

我这里的电脑是根节点,我不需要,我想要的就是

C:\Documents and Settings\Administrator\Desktop

已添加图片

这是我正在使用的功能

private void tvFolders_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) 
    { 
        //Populate folders and files when a folder is selected 
        this.Cursor = Cursors.WaitCursor; 
 
        //get current selected drive or folder 
        TreeNode nodeCurrent = e.Node; 
        string newPath = getFullPath(nodeCurrent.FullPath); 
        tbDirectory.Text = newPath; 
 
 
        //clear all sub-folders 
        nodeCurrent.Nodes.Clear(); 
 
        if (nodeCurrent.SelectedImageIndex == 0) 
        { 
            //Selected My Computer - repopulate drive list 
            PopulateDriveList(); 
        } 
        else 
        { 
            //populate sub-folders and folder files 
            PopulateDirectory(nodeCurrent, nodeCurrent.Nodes); 
        } 
        this.Cursor = Cursors.Default; 
    } 

请您参考如下方法:

在我看来,该代码中的 getFullPath 方法将完全满足您的要求。它去除 MyComputer\ 字符串并返回其余部分。写:

string newPath = getFullPath(nodeCurrent.FullPath);