Arjun PV
Arjun PV

Reputation: 41

C# - Get file/folder path from treeview node

I have 2 TreeViews in my winform. which contains my computers Drivelists.

Please help me to copy and move selected files/folders from one TreeView to other. I tried to use 2 strings as sourseDir and targetdir to store sourse and target path of my file. I tried to use the below code to access sourse and target path from TreeView nodes

string sourcedir = filelistleft.SelectedNode.FullPath.ToString();
string targetdir = filelistright.SelectedNode.FullPath.ToString();

foreach (var file in Directory.GetFiles(sourcedir))
     File.Copy(file, Path.Combine(targetdir, Path.GetFileName(file)), true);

But it throws an IOException The directory name is invalid. what can I do? thanks in advance...

Upvotes: 2

Views: 3069

Answers (1)

Ata Iravani
Ata Iravani

Reputation: 2206

I think because the FullPath property returns the node's relative path to tree view root,You'd better store tree view's root path's physical path in a variable, then add the selected node's FullPath to it.

string rootNodePhysicalPath = "C\\temp"; //assume c:\temp is tree view's root path
string selectedNodeFullPath  = sourcedir;
string selectedNodePhysicalPath = rootNodePhysicalPath + sourcedir;

Upvotes: 1

Related Questions