This article demonstrate how we can get the list of the files according specific search pattern with excluding some files. Suppose if a folder contains the following files:
“etest.xml” “summerytest.xml” “clstest.aspx”
“test1.xml” “test1.xml” “reporttest.xml”
“mytest.xml” “class.xml” “logtest.xml”
Let’s say we need to array of the all XML file names but don’t want to include the XML files starting with ‘test..’.
www.authorcode.com/search-for-an-element-in-array-with-array-find- method-in-c/
Solution:
For it we are using the Array.FindAll with System.Io.Direcotory.GetFiles function. And for excluding the files starting with ‘test.. we use the predicate(The Predicate is a delegate to a method that returns true if the object passed to it matches the conditions defined in the delegate). See the following example:
- private string[] GetSearchFiles(string folderpath)
- {
- string[] Searchfiles = Array.FindAll(System.IO.Directory.GetFiles(folderpath, "*.xml",
- System.IO.SearchOption.AllDirectories), IsInclude);
- return Searchfiles;
- }
- bool IsInclude(string s)
- {
- bool i = true;
- if (s.Contains("test"))
- {
- i=false;
- }
- return i;
- }
IsInclude procedure will call for each item in the array of the file names( from the System.IO.Directory.GetFiles) and if IsInclude function return false then Array.Find function exclude that file name.