Windows Develop Bookmark and Share   
 index > Windows Forms General > Treeview Problem
 

Treeview Problem

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.
meNu  Sunday, March 25, 2007 3:50 PM

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);

}

Bob zhu - SJTU  Tuesday, March 27, 2007 3:18 AM

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("\\")));

}

}

}

Bob zhu - SJTU  Monday, March 26, 2007 4:31 AM
SORRY GUYS

I have the method for copying files and folders, I NEED TO KNOW A WAY TO STRUCTURE THE TREEVIEW!
meNu  Monday, March 26, 2007 1:29 PM

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);

}

Bob zhu - SJTU  Tuesday, March 27, 2007 3:18 AM

You can use google to search for other answers

Custom Search

More Threads

• A simple problem with Application.Run()
• Windows form loading with form Back ground Image
• Form close event stupidity!
• Can anyone help me to make a shadow for forms (Once more)
• displaying pictures from textboxes
• information about the containing class?
• How can I compare enums?
• Map ListViewSubItem key(name) to a specific ListView's column name?
• Finding and selecting an item in a listview
• Intercepting file save/download in WebBrowser to add Custom save dialog