views:

188

answers:

1

i have a list box and i want to add a folder/directory to that which is at the specified location i have used the code

 string path = "E:\\shruti\\MyDir";
 DirectoryItem folder = new DirectoryItem(path);
 lstBurnItems.Items.Add(folder); //add folder to listbox

but its not working fine... what should i do to get success??

A: 

Below is an example showing how to add folders within a folder to a ListBox and to add Files within a folder into a ListBox. Sorry, you weren't clear so I gave both.

        string path = @"E:\shruti\MyDir";

        string[] dirs = Directory.GetDirectories(path);

        // For folders in the directory
        foreach(string dir in dirs)
            lstBurnItems.Items.Add(dir);


        // For files in the directory
        string[] dirFiles = Directory.GetFiles(path);

        foreach (string file in dirFiles)
            lstBurnItems.Items.Add(file);
Kyle Rozendo
thanks...i have one more question.. can we add the complete folder that is containing files in it to list box...MyDir is the folder that is containing some files and i want to add it to the listbox what is the method??
shruti
my listbox is blank initially...at run time i want to add complete folder to it..
shruti
Using the example code above, put the foreach (string file in dirFiles) into the foreach (string dir in dirs) loop and this will answer your question. It will loop through each folder and add all files to the listbox.
Ardman
why u have wriiten to add folders within a folder to a listbox?? i just want to add a single folder whose path i have specified..to a listbox..as an image..so that it should contain all the files that are within that folder.
shruti
As an image? Are you trying to build a tree?
Kyle Rozendo