user6364694
user6364694

Reputation:

C# : Get node name onClick in TreeView for a file manager

I have to create a file manager from scratch and im stuck in the begining.

It must show all drives name letter first. Then onclick shows folders an files in childnode and ... .

Here is my question:

  1. How can i get node name (as a string) which is clicked ?
  2. Is it the right way to doing this?

Here i first get drives name letter:

var drives = DriveInfo.GetDrives();
        for (var i = 0; i < drives.Count(); i++)
        {
            var drivesletter = drives[i].Name;
            treeView1.Nodes.Add(drivesletter);
        }

Here i created a method, when you click on each node, node name should be saved in a Variable, then it will get list of all files and folders in it and add them to the node that we clicked on it:

private void treeView1_Click(object sender, TreeViewEventArgs e)
    {
        var nodename = treeView1.Nodes.Find("*", true); //this line suppose to get clicked node name
        var getdirs = Directory.GetDirectories(nodename); //error: It says nodename isnt string type
        foreach (var getdir in getdirs)
        {
            treeView1.SelectedNode.Nodes.Add(getdir);
        }
    }

If you have any source, example or something simple like what im going to make, its a big help.

Upvotes: 1

Views: 1539

Answers (1)

Feras Al Sous
Feras Al Sous

Reputation: 1083

You can use this code to return Node Name:

protected void treeView1_AfterSelect (object sender,   
System.Windows.Forms.TreeViewEventArgs e)  
{  
   // Determine by checking the Text property.  
   MessageBox.Show(e.Node.Text);  
}  

Upvotes: 1

Related Questions