473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Listing subfolders and files from an main folder within treeview c#

14
Hi..

I am populating a treeview in form1 based on the checkeditems in checkedlistbox which is in form2 but am unable to list the subfolders and files of the checked folder..

Can anyone please help me????

Thanks..

hi..

This is my scenario but am not getting any error..

I have two forms form1 and form2,in form1 i have an treeview and in form2 i have checkedlistbox which i am populating as follows:-

Expand|Select|Wrap|Line Numbers
  1. System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("E:\\Mails");
  2.             System.IO.FileSystemInfo[] files = di.GetDirectories();
  3.             checkedListBox1.Items.AddRange(files);
and based on the checkedfolder i am building an treeview but am unbale to view the subfolders and files from the checked folder..
Dec 10 '11 #1

✓ answered by adriancs

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     foreach (var obj in checkedListBox1.CheckedItems)
  4.     {
  5.         PopulateTree("E:\\mail" + obj, treeView1.Nodes.Add(obj + ""));
  6.     }
  7. }
  8.  
  9. public void PopulateTree(string dir, TreeNode node)
  10. {
  11.     DirectoryInfo directory = new DirectoryInfo(dir);
  12.     foreach (DirectoryInfo d in directory.GetDirectories())
  13.     {
  14.         TreeNode t = new TreeNode(d.Name);
  15.         PopulateTree(d.FullName, t);
  16.         node.Nodes.Add(t);
  17.     }
  18.     foreach (FileInfo f in directory.GetFiles())
  19.     {
  20.         TreeNode t = new TreeNode(f.Name);
  21.         node.Nodes.Add(t);
  22.     }
  23. }

14 11965
adriancs
122 100+
hi, can u show the code where the error occurs?
Dec 11 '11 #2
bhagyap
14
Following is my code and am getting error for GetDirectories..

Could not find a part of the path 'E:\mails\System.Windows.Forms.CheckedListBox+Chec kedItemCollection'.



private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirecto ryInfo));
}

private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(direct ory));
foreach (var file in directoryInfo.GetFiles())
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}

Please help me...
Dec 12 '11 #3
adriancs
122 100+
...............................
Dec 12 '11 #4
adriancs
122 100+
You may want to try this:

resource: http://www.dreamincode.net/code/snippet2591.htm

Expand|Select|Wrap|Line Numbers
  1. public void PopulateTree(string dir, TreeNode node)
  2. {
  3.     DirectoryInfo directory = new DirectoryInfo(dir);
  4.     foreach (DirectoryInfo d in directory.GetDirectories())
  5.     {
  6.         TreeNode t = new TreeNode(d.Name);
  7.         PopulateTree(d.FullName, t);
  8.         node.Nodes.Add(t);
  9.     }
  10.     foreach (FileInfo f in directory.GetFiles())
  11.     {
  12.         TreeNode t = new TreeNode(f.Name);
  13.         node.Nodes.Add(t);
  14.     }
  15. }
Dec 12 '11 #5
adriancs
122 100+
can you show the codes (or blocks of code)>> How do you get this? From where do you get this?

Expand|Select|Wrap|Line Numbers
  1. 'E:\mails\System.Windows.Forms.CheckedListBox+Chec kedItemCollection'
It seems to be somewhere related to CheckedListBox.

How do you get the directory path from CheckedListBox?
Dec 12 '11 #6
bhagyap
14
I have overcome the exception which is as follows:-

foreach (var obj in opt.checkedListBox1.CheckedItems)
{
ListDirectory(treeView1, "E:\\mails\\" + obj);
treeView1.ExpandAll();
}
now the problem is i am unable to loop for all the checked folders..

Please guide me where i am going wrong..
Dec 12 '11 #7
adriancs
122 100+
I'm still trying to understand what you want.

You want to add all files in all checked folders from CheckedListBox into a TreeView Control, is it?
Dec 12 '11 #8
bhagyap
14
yes.. the checked folder should list its sub folders and files then display into treeview..
Dec 12 '11 #9
adriancs
122 100+
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     foreach (var obj in checkedListBox1.CheckedItems)
  4.     {
  5.         PopulateTree("E:\\mail" + obj, treeView1.Nodes.Add(obj + ""));
  6.     }
  7. }
  8.  
  9. public void PopulateTree(string dir, TreeNode node)
  10. {
  11.     DirectoryInfo directory = new DirectoryInfo(dir);
  12.     foreach (DirectoryInfo d in directory.GetDirectories())
  13.     {
  14.         TreeNode t = new TreeNode(d.Name);
  15.         PopulateTree(d.FullName, t);
  16.         node.Nodes.Add(t);
  17.     }
  18.     foreach (FileInfo f in directory.GetFiles())
  19.     {
  20.         TreeNode t = new TreeNode(f.Name);
  21.         node.Nodes.Add(t);
  22.     }
  23. }
Dec 13 '11 #10
bhagyap
14
Thank You.. but can u please help me in not repeating the folders into treeiew..
Dec 13 '11 #11
adriancs
122 100+
can you give an example of "not repeating the folders into treeview".
Dec 13 '11 #12
bhagyap
14
as i am populating the treeview from checkedlistbox i am retaining the state of it whenever the form loads so if i check a new folder and return to main form it populates the newly checked folders as well as previously checked..

And how can i add root node to this??

Thank You!!!
Dec 13 '11 #13
adriancs
122 100+
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     treeView1.Nodes.Clear();
  4.     treeView1.Nodes.Add("Root");
  5.     foreach (var obj in checkedListBox1.CheckedItems)
  6.     {
  7.         PopulateTree("F:\\" + obj, treeView1.Nodes[0]);
  8.     }
  9. }
Dec 14 '11 #14
bhagyap
14
Thank You.. Ur code is very heplful to me..
Dec 14 '11 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Craig Keightley | last post by:
I am trying to write a function to copy an existing folder and folder structure but am already stuck. What I want to do is the following: Copy a default folder (default_folder) and all sub...
8
by: vinesh | last post by:
I have sample Asp.Net Web Application project. Let me know how to keep the files related to this project (like the webform.aspx, WebForm1.aspx.vb, WebForm1.aspx.resx) in a separate folder within a...
6
by: ManagedCoder | last post by:
Hi, My requirement is as follows: I need to set the HttpExpires (enable content expiration - set to 7 days) on a folder within a virtual directory. I have been able to set the HttpExpires...
5
by: akmsikhan | last post by:
I am using a Toshiba Satellite M115 Series Laptop. Recently I was trying to see some hidden files but I can't. When I try to enable Show Hidden Files and Folder it is not enabling. Each time I click...
14
by: prashant | last post by:
hello, I am looking to determine the number of image files in a folder so that I could create a dynamic table accordingly. For this i need a method to identify the number of files in a folder. Is...
1
by: 7effrey | last post by:
Does anyone have a script that can count the number of files in a folder + subfolders and add it together?
6
by: deve8ore | last post by:
Hello, We have a vendor that will supply us many files, and unfortunately will place them in different folders with no uniformity (within Windows Explorer). I'd like to have the capability to...
4
by: kanishka1213 | last post by:
i am trying to make a script that recognizes all unparsed stream files from folder and runs an external tool in windows system to parse them. so far i am able to call that tool for all unparsed...
11
by: Etienne1 | last post by:
Hello, I have a report with a subreport that has a subreport that as a subreport and so on. I have so far 6 levels and each of them is linked to his parent. What I'm trying to do is when a new...
0
by: Arulmanoj | last post by:
Hi, I don't know whether it is the right place to ask this question. But I am facing a problem. I need write a batch file which will delete files which are 5 days older from current system date in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.