meNu

I am creating an application which copies files from one place to another. At the same time I am trying to add the filenames to the treeview. The problem is that I am trying to create the same file structure as the file.

i.e. MyDocuments -> A-> B -> C

So the Treeview will show MyDocuments as the ROOT, then A as the first child node, then B as a child node of A and then finally C as a child node as B.


Re: Windows Forms General Treeview Problem

Bob zhu - MSFT

Hi meNu here we got sample form codeproject and quite similar to yours

http://www.codeproject.com/csharp/my_explorer.asp

and for copy folder to folder I have one method easy to use

private void button1_Click(object sender, System.EventArgs e)

{

CopyFiles(@"D:\PCT", @"E:\PCT");

}

private void CopyFiles(string varFromDirectory, string varToDirectory)

{

Directory.CreateDirectory(varToDirectory);

if (!Directory.Exists(varFromDirectory)) return;

string[] directories = Directory.GetDirectories(varFromDirectory);

if (directories.Length > 0)

{

foreach (string d in directories)

{

CopyFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));

}

}

string[] files = Directory.GetFiles(varFromDirectory);

if (files.Length > 0)

{

foreach (string s in files)

{

File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\")));

}

}

}






Re: Windows Forms General Treeview Problem

meNu

SORRY GUYS

I have the method for copying files and folders, I NEED TO KNOW A WAY TO STRUCTURE THE TREEVIEW!




Re: Windows Forms General Treeview Problem

Bob zhu - MSFT

you can use follow code:

private void loadtotree(string dirroot,TreeNode parent)

{

TreeNode node=new TreeNode(dirroot);

if(parent==null)

{this.treeView1.Nodes.Add(node);}

else

{parent.Nodes.Add(node);}

string[] directories = Directory.GetDirectories(dirroot);

foreach(string dir in directories)

{

string[] sub_dirs=Directory.GetDirectories(dir);

if(sub_dirs.Length==0)

{

node.Nodes.Add(dir);

string[] files=Directory.GetFiles(dir);

}

else

{

TreeNode subnode=new TreeNode(dir);

node.Nodes.Add(subnode);

foreach(string sub_dir2 in sub_dirs)

{

loadtotree(sub_dir2,subnode);

}

}

}

}

private void Form1_Load(object sender, System.EventArgs e)

{

this.loadtotree("C:\\Documents and Settings\\mei\\My Documents\\Visual Studio Projects\\CS",null);

}