Hi,
May I get some help on this topic?
How to recursive search in dir (sub-dir) under a root directory (C:\)
For example
Search file pattern is *.txt
Root dir is C:\
This program will search all sub dir in root and
If found add file name to the array list
In vb.net code
Thanks..
Tomcy
tomcy thomas2 pointsPosted Oct 5, 2009, 9:55 AM EDT
using System; using System.Text; using System.Windows.Forms; using System.Collections; using System.IO; namespace RecursiveSearch { public partial class Form1 : Form { public string FilePattern = "*.sys"; public string SearchPath = @"C:\"; //public string SearchPath = @"C:\Documents and Settings\net11\Desktop\Russia"; public string ListingFileName = @"c:\fileList.txt"; ArrayList al = new ArrayList(); public bool Recursive = true; StringBuilder sbErrorLogs = new StringBuilder(); public string LogFileName = @"c:\fileLog.txt"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } #region Recursive method for to Search Fles private void RecursiveSearchingForFles(ref string strDirectory, ref ArrayList array, ref string DirPattern, bool Recursive) { DirectoryInfo dirInfo = new DirectoryInfo(strDirectory); FileInfo[] pFileInfo = new FileInfo[0]; Application.DoEvents(); try { pFileInfo = dirInfo.GetFiles(DirPattern); //getting all files from the folder } catch (UnauthorizedAccessException ex) { //even this exception from any folder, the recusive program will move to next folder //MessageBox.Show("Unauthorized Access to Folder/File!", ex.Message); return; sbErrorLogs.AppendLine("from 1st catch : "+ex.Message + " : " + ex.Source); return; } array.AddRange(pFileInfo);// to increase the capacity of array DirectoryInfo[] pdirInfo; pdirInfo = dirInfo.GetDirectories(); if (Recursive == true)//if recursive is true only it will go for recursive else will take the files from the same folder { Application.DoEvents();// this is to reduce system hanging foreach (DirectoryInfo dirIter in pdirInfo) { strDirectory = dirIter.FullName; RecursiveSearchingForFles(ref strDirectory, ref array, ref DirPattern, Recursive); } } } #endregion private void button1_Click(object sender, EventArgs e) { sbErrorLogs.Length = 0; // the list of all files updated in the array called al. RecursiveSearchingForFles(ref SearchPath, ref al, ref FilePattern, Recursive); //label1.Text = "Total File count=" + al.Count.ToString(); StringBuilder sb = new StringBuilder(); foreach (FileInfo fn in al) { sb.AppendLine(fn.FullName); } StreamWriter sw = new StreamWriter(ListingFileName); sw.Write(sb.ToString()); sw.Close(); sw.Dispose(); StreamWriter LogFile = new StreamWriter(LogFileName); LogFile.Write(sbErrorLogs.ToString()); LogFile.Close(); LogFile.Dispose(); MessageBox.Show("File Searching Over"); } } }