Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello.. friends i need some help.. regarding the folders..
i have used a folderbrowsedilog and selected a folder and that folder contains some more folders..
now my task is .. to get files from first folder to listbox1 and second folder to listbox2.. and so on..

for example..
the first folder contains 10 files, and i want 10 files to be added to listbox1;
the second folder contains 6 files , i want 6 files to be added to listbox2;
..
.
.

is it possible..


here is my code ... try to make some modifications in it..
C#
var fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;

            if (fbd.ShowDialog() == DialogResult.Cancel)
                return;

            string foldername = fbd.SelectedPath;

            var dirs = from dir in Directory.EnumerateDirectories(foldername )
                       select dir;

            foreach (var dir in dirs)
            {
                foreach (string f in Directory.EnumerateFiles(dir, "*.*"))
                  listBox1.Items.Add(f);

            }
Posted
Comments
Debabrata_Das 13-Jun-14 0:50am    
Hello friend, There could be n number of levels in a folder structure. As you mentioned that you want to populate List controls with files from each folder in each level. Since you never know the value - n, how are you planning to create List controls? Are you creating List controls dynamically?
- DD
maheshwarreddy009 13-Jun-14 0:53am    
yes i want to create listboxes dynamically.. the code given was just sample..
do u know the method to solve this??
maheshwarreddy009 13-Jun-14 0:54am    
and also there may be mostly 5 folder for this project inside it..
Debabrata_Das 13-Jun-14 1:02am    
Ok. So if you consider Folder structure is nothing but a Tree structure where root node is the top level (n) folder. From root, you have to traverse to it's child node (i.e. n-1 level) and get all the files. Then you have to move to it's child node (i.e n-2 level) to get the files and so on...

I tried to provide a logical solution as I have never tried such problem. Please let us know if you face any challenges to implement it.
- DD
maheshwarreddy009 13-Jun-14 1:12am    
can we select the folders by their index ?

Hi, I tried to create a small POC (Proof of concept) using the following code:

Considering the following folder structure:

FolderA
|_ A1.txt
|_ A2.txt
|_ FolderB
|_ B1.txt
|_ B2.txt
|_ B3.txt

C#
string[] files = Directory.GetFiles(@"D:\FolderA");
ListBox1.DataSource = files;
ListBox1.DataBind();

string[] folders = Directory.GetDirectories(@"D:\FolderA");
files = Directory.GetFiles(folders[0]);
ListBox2.DataSource = files;
ListBox2.DataBind();


Output would be as follows:

ListBox1:
A1.txt
A2.txt

ListBox2:
B1.txt
B2.txt
B3.txt
 
Share this answer
 
Comments
maheshwarreddy009 13-Jun-14 2:19am    
@ Debabrata_Das.. thank you.. but i solved that and i think i posted.. also.. thank you anyway for giving me idea ..
here i go i got the code and it works perfectly.. mostly thanks to Debabrata_DAs as he gave me idea..



C#
var fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = false;

if (fbd.ShowDialog() == DialogResult.Cancel)
return;

string foldername = fbd.SelectedPath;


var dirs = from dir in
Directory.EnumerateDirectories(@" "+foldername )
select dir;
int directoryCount;

foreach (var dir in dirs)
{
foreach (string f in Directory.EnumerateFiles(dir, "*.*"))
{
listBox1.Items.Add(f);


}
directoryCount = System.IO.Directory.GetFiles(@"" + dir).Length;
var lb = new ListBox();
lb.Dock = DockStyle.Bottom;
foreach (string f in Directory.EnumerateFiles (dir,"*.*"))
lb.Items.Add(f);
panel2.Controls.Add(lb);
string m = directoryCount.ToString();
MessageBox.Show(m);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900