C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » File Operations »

Count your subfolder


Posted Date: 19 Aug 2009    Resource Type: Code Snippets    Category: File Operations
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 8



Description :


Count the sub folders in a folded.
Following code will count how many sub folders in the folder

Following are the namespaces used

using System;
using System.IO;

Code Segment

public class DirectoryCounter {
public static void Main() {
DirectoryInfo MyDirectoryInfo = new DirectoryInfo(@"c:\Program Files");
int Mytotalcount = CountDirectories(MyDirectoryInfo);
Console.WriteLine(Mytotalcount);
}

public static int CountDirectories(DirectoryInfo dir) {
DirectoryInfo[] MySubdirs = dir.GetDirectories();
int count = MySubdirs.Length;
foreach (DirectoryInfo subDir in MySubdirs) {
count += CountDirectories(subDir);
}
return count;
}
}


Code Explanation

1. Create instance of the DirectoryInfo.
2. call the method CountDirectories
3. In the method CountDirectories get all the sub folders.
4. using foreach get count of the folders


By
Nathan



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Count your subfolder  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Use of System.Diagnostics.Process
Previous Resource: Search the files from the directory
Return to Discussion Resource Index
Post New Resource
Category: File Operations


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use