none
Directory.GetFiles with searchPattern to get all *.dll and *.exe files in one call

    Question

  • Hi experts,

    is it possible to use Directory.GetFiles with searchPattern to get all *.dll and *.exe files in one call?

     

    Thanks

    Avi

    Sunday, February 18, 2007 2:04 PM

Answers

  • No, but you can of course write your own ;)

    GetFiles(@"C:\Windows", "*.exe|*.dll", SearchOption.TopDirectoryOnly);

    public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
    {
       
    string[] searchPatterns = searchPattern.Split('|');
       
    List<string> files = new List<string>();
       
    foreach (string sp in searchPatterns)
            files.AddRange(System.IO.
    Directory.GetFiles(path, sp, searchOption));
        files.Sort();
       
    return files.ToArray();
    }

    Sunday, February 18, 2007 11:30 PM

All replies

  • No, but you can of course write your own ;)

    GetFiles(@"C:\Windows", "*.exe|*.dll", SearchOption.TopDirectoryOnly);

    public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
    {
       
    string[] searchPatterns = searchPattern.Split('|');
       
    List<string> files = new List<string>();
       
    foreach (string sp in searchPatterns)
            files.AddRange(System.IO.
    Directory.GetFiles(path, sp, searchOption));
        files.Sort();
       
    return files.ToArray();
    }

    Sunday, February 18, 2007 11:30 PM
  • Sir, can you convert your code to vb .net as i'm new to .net programming. thanks in advance.
    Sunday, August 30, 2009 6:14 AM
  • Not necessary.  VB has a multiple filter search option.  My.Computer.FileSystem.GetFiles Method

    Sunday, August 30, 2009 6:39 AM
  • Sir, I'd like to search for all *.tif , *.jpg, *.pdf to be added into my listview not only *.tif. please check below.

     

    For Each Item As String In Directory.GetFiles(e.Node.Tag, "*.tif", SearchOption.TopDirectoryOnly)

    ListView1.Items.Add(Item)

     

    Next

    Sunday, August 30, 2009 6:46 AM
  • And your problem is?
    Sunday, August 30, 2009 6:57 AM
  • I cannot do it, just like john doe said I need to write on my own. I need all *.tif, *.jpg and *.pdf to be in my listview. I cannot create my own.
    Sunday, August 30, 2009 7:03 AM
  • Is there a problem with the link?  It works OK for me.  Please explain the problem you are having.  Repeating the question I answered doesn't help.
    Sunday, August 30, 2009 7:22 AM
  • I'm new to .net programming. I cannot do it. Anyway, thanks.
    Sunday, August 30, 2009 7:33 AM
  • What can't you do?  Can you click the link?  Can you type "My.Computer.FileSystem.GetFiles" in your code and follow the help provided by Intellisense?  Can you find the method in your help files?
    Sunday, August 30, 2009 7:43 AM
  • Hi every one,

    You can write this code for serach in multiply filters:

    private static string[] GetFiles(string sourceFolder, string filters, System.IO.SearchOption searchOption)
    {
           return filters.Split('|').SelectMany(filter => System.IO.Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray();
    }

    • Proposed as answer by 2raj Monday, November 4, 2013 11:42 AM
    Saturday, November 13, 2010 2:45 PM