简体   繁体   中英

C# get folderpath from TreeListNode in treeList devexpress

I use XtratreeList with fileExplorerAssistant. I have a problem when I want to get the path of the selected folder from treelist. Or I have a problem in getting the folder path from TreeListNode. Please help me.

My code is:

        private void frmMovieAddAuto_Load(object sender, EventArgs e)
    {
        // Scan for all partitions
        System.IO.DriveInfo[] driveList = System.IO.DriveInfo.GetDrives();
        foreach (var drive in driveList)
        {
            // Select only logical fixed partitions
            if (drive.DriveType == System.IO.DriveType.Fixed && drive.IsReady)
            {
                // Add each drive as a root node
                treeListExtension1.RootNodes.Add(new PathNode(drive.RootDirectory.ToString()));
            }
        }
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        TreeListColumn columnname;
        columnname = treeList1.Columns[0];
        columnname.Caption = "Folder Name";

        List<TreeListNode> nodes = treeList1.GetNodeList();
        foreach (TreeListNode node in nodes)
        {
            if (node.Checked == true)
            {
                DirectoryInfo di = new DirectoryInfo(node.GetValue(columnname).ToString());

                foreach (FileInfo fi in di.GetFiles("*.avi;*.mpg;*.mpeg;*.mp4;*.mkv;*.divx;*.AVI;*.MPG;*.MPEG;*.MP4;*.MKV;*.DIVX", SearchOption.AllDirectories))
                {
                    //do something
                }
            }

        }
    }

In this code:

        private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
    {
        // Get the node that is currently focused (selected) in the TreeList
        TreeListNode focusedNode = treeList1.FocusedNode;

        // Get the column that contains the folder path
        TreeListColumn folderPathColumn = treeList1.Columns[0];

        // Get the value of the folder path column from the focused node
        string folderPath = focusedNode.GetValue(folderPathColumn).ToString();

        if(focusedNode.Checked == true)
        {
            pathfolderList.Add(folderPath);
        }
    }

In this line code:

string folderPath = focusedNode.GetValue(folderPathColumn).ToString();

It gives this error:

'Object reference not set to an instance of an object.'

Your question is how to get the full path of a DevExpress.XtraTreeList.Nodes.TreeListNode . This requires traversing the DisplayText from the selected node up to its root, When this collection is reversed and combined, the result is the full path.

foreach (var node in treeList1.GetNodeList())
{
    List<string> builder = new List<string>();
    TreeListNode traverse = node;
    // This adds the display text from the leaf to the root.
    while(traverse != null)
    {
        builder.Add(traverse.GetDisplayText(0));
        traverse = traverse.ParentNode;
    }
    // What we want is the root to the leaf, so reverse the list.
    builder.Reverse();
    // Now combine into a path.
    var path = Path.Combine(builder.ToArray());
    Debug.WriteLine(path);
}

调试输出

You might also find the DevExpress documentation helpful (their approach is slightly different).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM