Find File Content Using the .NET IO Class | CodeGuru

Find File Content Using the .NET IO Class

Environment: C#, Win9x/2000/XP This is a very simple application that uses the .NET I/O class to search the files’ content in a directory. I first used a DirectoryInfo class to get the directory information. Then, I saved the file list in a FileInfo array. You loop through the array and open each file for reading. […]

Written By
CodeGuru Staff
CodeGuru Staff
Jul 18, 2003
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: C#, Win9x/2000/XP

This is a very simple application that uses the .NET I/O class to search the files’ content in a directory. I first used a DirectoryInfo class to get the directory information. Then, I saved the file list in a FileInfo array. You loop through the array and open each file for reading. Convert the buffer to string type, find the text, and raise an event when it’s found.

The searching process can be time consuming. We can use the .NET delegate function to search asynchronously, as show below.

//declare a delegate function
private delegate void FindAsyn();
[STAThread]
static void Main(string[] args)
{
  FileFind ff = new FileFind();
  //subscribe the found file event
  ff.OnFileFound += new FoundDelegate(OnFoundFile);
  //The directory to be searched
  ff.PathName = @"C:MyFileDir";
  //The text to be searched
  ff.FindText ="findtext";
  //delegate to...
  FindAsyn f = new FindAsyn(ff.StartSearch);
  //invoke the searching function asynchronously
  f.BeginInvoke(null,null);
  Console.Read();             // pause the screen;
  FileFind.bStopIt = true;    // stop it if the process takes
                              // too long.
  Console.Read();
  //unsubscribe the event
  ff.OnFileFound -= new FoundDelegate(OnFoundFile);
}
private static void OnFoundFile(string filename)
{
  Console.WriteLine(filename);
}

Downloads


Download demo project - 7 Kb


Download source - 3 Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.