Searching Directories?

Im trying to search specific directories by file name (and maybe content), but have no idea hwo to do this


Answer this question

Searching Directories?

  • Bimlesh

    There's no easy way to search inside the file, as the routine requires to determine the type of file between textonly and binary; searching textonly file would be easy, since we can simply open it using TextFileReader and read the entire content then perform the search. However, searching for patterns in binary files is different (specially if it includes compressed files), and there are many approach (algorithm) for searching patterns inside binary files which are none of these are easy to implement. For both case, the search routines will be opening the files and look inside it (if it is a compressed files, then look inside the archive and search the files inside it one by one).

    Regards,

    -chris

  • kbradl1

    You can use the GetFiles method of System.IO.Directory class to search for files:



    string [] files = System.IO.Directory.GetFiles(@"C:\path", "*.*");



    The first parameter is the path you want to search, and the second parameter is the wildcard for the filenames you are searching for. The result will be an array of string.

    Regards,

    -chris

  • ckramer

    Here is a little example:


    public void StartSearch()
    {
    DirectoryInfo root = new DirectoryInfo( @"c:\myfolder\" );
    SearchDirectory( root );
    }

    public void SearchDirectory( DirectoryInfo root )
    {
    FileInfo[] files = root.GetFiles();
    DirectoryInfo[] subDirectories = root.GetDirectories();

    foreach(FileInfo file in files)
    {
    // TODO: Do compaire logic here.
    if( file.Length > 1024 )
    {
    MessageBox.Show( this, "We found a file greater then 1024 bytes." );
    }
    }

    foreach( DirectoryInfo directory in subDirectories )
    {
    SearchDirectory( directory );
    }
    }




  • traitors

    Chris Vega, this is indeed a easy way to search. Thanks for you reply!

    But the topic started want to lookup the file content as well meybe and you solution can't be progressed async or with some event when searching of how much percent is completed.

    When you don't need all this fancy stuff, i suggest you use the example Chris Vega posted.


  • Fahad Habib

    be a little more specific so i can help you.. You want something like the windows search

    when it comes to files and directories the System.IO namespace has all the classes that u need!




  • Searching Directories?