Searching recursively through a folder for checking the files and folders stored in it

I want to develop an application in c# using visual studio in which if I select a particular folder from the computer then it must list all the sub folders and files stored in that selected folder in the form of lists.
Abhishek ModakStudentAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Abhishek ModakStudentAuthor Commented:
If someone knows the procedure to do it in c#, please help me out.
0
MishaProgrammerCommented:
If I understand you, you want to get all Directories programmatically.

Try this code:

            string[] dirs = Directory.GetDirectories(@"c:\", "p*");
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }

Open in new window

0
Abhishek ModakStudentAuthor Commented:
I also want the path of  files in the selected directory as well as the path of  files contained in the subfolders contained in the selected directory.. Basically,I want to store paths of all files and directories of a particular selected folder in different lists. That is, one list for storing paths of all files that are discovered in selected folder as well as subfolders. And another list that stores paths of all discovered subfolders. And I want this list to be printed on the console.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

MishaProgrammerCommented:
			string path = @"D:\My_Work";
			string[] all_files = Directory.GetFiles(path); // array of all files
			string[] all_direcroties = Directory.GetDirectories(path); // array of all subdirectories
			foreach (string path_file in all_files)
			{// print full file names
				Console.WriteLine(path_file);
			}
			foreach (string sub_dir in all_direcroties)
			{// printsubdirectories
				Console.WriteLine(sub_dir);
			}

Open in new window

0
Shaun VermaakTechnical Specialist/DeveloperCommented:
static void DirSearch(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                Console.WriteLine(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}

Open in new window

https://stackoverflow.com/questions/929276/how-to-recursively-list-all-the-files-in-a-directory-in-c
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abhishek ModakStudentAuthor Commented:
I tried the DirSearch function to print files contained in a specified folder,but it prints nothing on the console. I have tried the following code..please check if i have done something wrong.
 private void btn_Browse_Click(object sender, EventArgs e)
        {
          FolderBrowserDialog open = new FolderBrowserDialog();
            open.ShowDialog();
            txtLocalEncFile.Text = open.SelectedPath;// txtLocalEncFile is a textbox where I print the file path of the folder whose subdirectories
                                                                                         and files i want to list on the console
            string path = txtLocalEncFile.Text;
            Console.WriteLine(path);
            DirSearch(path);
          }
 static void DirSearch(string sDir)
{
    try
    {
        foreach (string d in Directory.GetDirectories(sDir))
        {
            foreach (string f in Directory.GetFiles(d))
            {
                Console.WriteLine(f);
            }
            DirSearch(d);
        }
    }
    catch (System.Exception excpt)
    {
        Console.WriteLine(excpt.Message);
    }
}
Suppose I have selected a path C:\Users\HP\Desktop\Test_Folder_1 and this folder contains an image file, pdf file, ppt file and a rar file. Then it must display the file names path in that folder also on the console. I tried debugging the code, but i think the code is not moving inside either try or catch blocks. What should I do now?
0
MishaProgrammerCommented:
There is no inforamtion on console because you write your application on Window Forms.
I see you use event btn_Browse_Click.
If your really want to print information on console, you should add this code

	
using System.Runtime.InteropServices;
private void Form1_Load(object sender, EventArgs e) // Load your form event
{
		AllocConsole();
}
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool AllocConsole();

Open in new window


Alternatively you can create Console Application and put your code to Main method or if you want to use Window Forms, you can print information in file
0
Abhishek ModakStudentAuthor Commented:
I tried inserting another folder in the Test_Folder_1  named Dummy and inserted  some of the files in the Dummy folder. The console now displays the file path of the files in the the subdirectory Dummy and path of the subdirectory Dummy. But I am not getting the path of the files that are present in the Test_Folder_1 . How to display them as well on the console.
0
Abhishek ModakStudentAuthor Commented:
Suppose that my Test_Folder_1 contains Dummy subdirectory and 1 image file and 1 pdf file. My Dummy subdirectory contains a ppt file and rar file. Console displays the subdirectory file paths as well as the subdirectory file path. But I also want to print the path of the image file and pdf file of the Test_Folder_1 directory. What changes should I make in the code to be able to display them as well on the console?
0
MishaProgrammerCommented:
No problem, add code , which print files in base directory:
Console.WriteLine(path);
foreach (string f in Directory.GetFiles(path))
{
          Console.WriteLine(f);
}
DirSearch(path);

Open in new window

0
Abhishek ModakStudentAuthor Commented:
Thanks misha. The code worked. Now I am getting the path of the files in Test_Folder_1 as well.
0
Abhishek ModakStudentAuthor Commented:
Thanks for your help.
0
MishaProgrammerCommented:
glad to help you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.