Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to search logic for seaching any file in System using c#:confused:.
E.g. Search option in windows xp.
Posted

See this msdn article that does exactly what you want:
http://support.microsoft.com/kb/303974/EN-US[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Feb-11 13:05pm    
Will do - a 5.
--SA
 
Share this answer
 
C#
public static void Search(string dirpath, string searchpattern)
        {
            // 'dirpath' shoud be any path like "C:\\documents\\" or "D:\\temp" etc
            // 'searchpattern' is nothing should be *.txt or *.xml or *.* and so-on
            string[] sFiles = Directory.GetFiles(dirpath, searchpattern);
            foreach (string sFile in sFiles)
            {
                // Result files for the search pattern will be here ....
            }
            string[] sDirs = Directory.GetDirectories(dirpath);
            foreach (string sdir in sDirs)
            {
                // This method will Recursive do check to the sub directories. with the same search pattern
                Search(sdir, searchpattern);
            }
        }
 
Share this answer
 
Comments
johannesnestler 1-Feb-11 9:26am    
Hi Vasanth,
I Agree with your solution if you have to use .NET CF or old Framework version.
But for the recursive search there is an overload in the other Framework versions for GetFiles:
string[] astrFiles = System.IO.Directory.GetFiles(strPath, strSearchPattern, SearchOption.AllDirectories);
Vasanth@n 1-Feb-11 12:32pm    
Yes, There is a Overload method, I knew it yaar. anyway thanks for your information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900