Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I get a list of all files and folders of a directory in a grid view in the form as shown below using c# windows .
ParentFolder     ChildFolder     Filename
AAAA             aaaaaa          aaa.txt


Thanks
Rahul
Posted
Updated 7-May-13 20:58pm
v2

The DirectoryInfo[^] class provides all you need, namely the GetFiles, GetDirectories methods and the Parent property.
 
Share this answer
 
Use the System.IO.Directory.GetFiles[^] and GetDirectories[^] methods to retrieve the information.
Then you can display it in a DataGridView[^] just like any other pile of strings.
C#
MyDataGridView.Rows[whateverRowNumber].Columns["ChildFolderNameColumn"].Value = childFolderName;

Or you can store your data in a BindingList[^] and bind it to the DataGridView.
Depending on your very situation, it can be less hassle than the first option.
 
Share this answer
 
private void btnGetFolders_Click(object sender, EventArgs e)
        {
            string[] dirs = Directory.GetDirectories(@"DirectoryPath eg. E:\Music");
            
            foreach (string dir in dirs)
            {
                lstMusic.Items.Add(dir); //Adding the list of files and folders to a listbox called lstMusic           
            }

        }
 
Share this answer
 
Comments
Rahul Krishnan R 8-May-13 5:02am    
Hi,
Thanks for your reply.
In my case, the user will just give the directory like D:\Music and the system should give all the files, sub folders and files inside the sub folders separately in different columns as shown in my question.

Thanks

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