How to loop through all the files in a directory in c # .net?

C#.Net

C# Problem Overview


I want to get the files in folder and also in its subfolders.The following code does not get the files in its subfolder:

string[] files = Directory.GetFiles(txtFolderPath.Text, "*ProfileHandler.cs");

Can anyone Please tell me how to implement this in c# .net?

C# Solutions


Solution 1 - C#

string[] files = 
    Directory.GetFiles(txtPath.Text, "*ProfileHandler.cs", SearchOption.AllDirectories);

That last parameter effects exactly what you're referring to. Set it to AllDirectories for every file including in subfolders, and set it to TopDirectoryOnly if you only want to search in the directory given and not subfolders.

Refer to MDSN for details: https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx

Solution 2 - C#

try below code

Directory.GetFiles(txtFolderPath.Text, "*ProfileHandler.cs",SearchOption.AllDirectories)

Solution 3 - C#

You can have a look at this page showing Deep Folder Copy, it uses recursive means to iterate throught the files and has some really nice tips, like filtering techniques etc.

http://www.codeproject.com/Tips/512208/Folder-Directory-Deep-Copy-including-sub-directori

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPremView Question on Stackoverflow
Solution 1 - C#MigwellView Answer on Stackoverflow
Solution 2 - C#TalentTunerView Answer on Stackoverflow
Solution 3 - C#JoezerView Answer on Stackoverflow