1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Count Subdirectories

Discussion in 'C#' started by aranjan, Jul 20, 2010.

  1. aranjan

    aranjan New Member

    Joined:
    Jul 20, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    How to Count Subdirectories in a given directory in C#
     
  2. imported_Sohaib

    imported_Sohaib New Member

    Joined:
    Aug 26, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;                                 //add this namespace
    
    //add a button with name btnSelectPath
    //add a button with name btnCountSubDir
    //add a FolderBrowserDialog with name FolderBrowser
    //add a ListBox with name txtFolderPath
    //add a TextBox with name txtFolderPath
    //add two Label one of those with name lblCount
    
    namespace CountSubdirectories
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnSelectPath_Click(object sender, EventArgs e) 
            {
                FolderBrowser.ShowDialog();
                txtFolderPath.Text = FolderBrowser.SelectedPath;
            }
    
            private void btnCountSubDir_Click(object sender, EventArgs e)
            {
                string[] subdir = Directory.GetDirectories(txtFolderPath.Text);
                foreach (string s in subdir)
                    lstSubDir.Items.Add(s);
                lblCount.Text = lstSubDir.Items.Count.ToString();
            }
    
        }
    }
     

Share This Page