.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

looping through a folder

Posted By:nishithraj       Posted Date: December 24, 2009    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  
 

Following code will help you to loop though a folder and to identify the subfolders too.

public void Main() 
{     
     GetListOfSubFolders("C:\\FolderToCheck");     
     Console.ReadLine(); 
} 
private void GetListOfSubFolders(string strRootDir)
{
string strSubDir = null;
try
{
//Check the directory existence 
if (Directory.Exists(strRootDir) == true)
{
//Loop through the Directories 
foreach (var strSubDir in Directory.GetDirectories(strRootDir))
{
//Writing the directory path 
Console.WriteLine(strSubDir);
//Recursive call to loop through the subfolder's subfolders if any. 
GetListOfSubFolders(strSubDir);
}
}
}
catch (Exception ex)
finally
{
//Handle exception 
}
} 

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend