Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


search files by keyword/wildcard in csharp

Posted on 2010-01-10
Medium Priority
Last Modified: 2013-12-17
Dear experts,

I would like to write just a really small console application which searches for files (like when you search in windows explorer) by keyword. Can I somehow use the built in search capabilities in Windows? Because I already found a code to do file search, but that code only search that particular directory WITHOUT subdirectories :(
I would need two output: the name of the file and the path of the file. Just these two.

Thank you a lot for your help,
Question by:mkrisz
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 15

Accepted Solution

x77 earned 1600 total points
ID: 26278247
Create your own Function to Recursive Find files

            List<string> Result = new List<string>(100);
            RecursiveFind(@"c:\", @"*.Exe", Result);

public void RecursiveFind(String Path, String Mask, List <string> Result)
                foreach (string f in System.IO.Directory.GetFiles(Path, Mask)) Result.Add(f);
            catch { }

            foreach (string d in System.IO.Directory.GetDirectories(Path))
                RecursiveFind(d, Mask, Result);

Open in new window

LVL 15

Expert Comment

ID: 26278275
To get DirectoryPath / FileName:

            foreach (string f in Result)
                string PathName = System.IO.Path.GetDirectoryName(f);
                string FileName = System.IO.Path.GetFileName(f);

LVL 11

Assisted Solution

lenordiste earned 400 total points
ID: 26278664
here is a nice article from MSDN explaining how to do file searches in LINQ:

also, here are some more file search futures you can work on:

I use something similar to the top link in production and it works like a charm !
LVL 11

Expert Comment

ID: 26278667
I meant "features" not "futures"... :)

Author Closing Comment

ID: 31675187

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Introduction to Processes

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question