File Search Library
SearchFilesAsync Method
Example 



View with Navigation Tools
KellermanSoftware.FileSearchLibrary Namespace > FileSearchLogic Class : SearchFilesAsync Method
Search Parameters
Search for files asynchronously
Syntax
'Declaration
 
Public Function SearchFilesAsync( _
   ByVal searchParms As SearchParms _
) As Task
 
'Usage
 
Dim instance As FileSearchLogic
Dim searchParms As SearchParms
Dim value As Task
 
value = instance.SearchFilesAsync(searchParms)

Parameters

searchParms
Search Parameters

Return Value

A list of matched files
Example
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KellermanSoftware.FileSearchLibrary;
using NUnit.Framework;
 
namespace KellermanSoftware.FileSearchLibraryExample
{
    [TestFixture]
    public class SearchFilesAsyncExample
    {
        [Test]
        public void SearchFilesAsync()
        {
            //Setup the search parameters
            SearchParms parms = new SearchParms();
 
            //This is the directory to search
            parms.DirectoriesToSearch.Add(@"c:\_git");
 
            //Include all C# files by wildcard
            parms.FilesToInclude.Add("*.cs");
            parms.FileIncludeSearchType = FileSearchType.Wildcard;
 
            //Search for a plain text phrase
            parms.SearchString = "public partial class";
            parms.SearchExpressionType = SearchExpressionType.PlainText;
 
            //Attach Events
            parms.CompleteEvent += Complete;
            parms.FileFoundEvent += FileFound;
            parms.ExceptionEvent += ExceptionEvent;
            parms.ProgressEvent += ProgressEvent;
            
            //Search with the parameters
            FileSearchLogic fileSearchLogic = new FileSearchLogic();
            var myTask = fileSearchLogic.SearchFilesAsync(parms);
            
            Task.WaitAll(myTask);
 
            //Detach Events
            parms.CompleteEvent -= Complete;
            parms.FileFoundEvent -= FileFound;
            parms.ExceptionEvent -= ExceptionEvent;
            parms.ProgressEvent -= ProgressEvent;
        }
 
        public void Complete(object sender, List<string> files)
        {
            Console.WriteLine("Complete".PadRight(40,'-'));
            foreach (var file in files)
            {
                Console.WriteLine(file);
            }
        }
 
        public void FileFound(object sender, string file)
        {
            Console.WriteLine("File Found: " + file);
        }
 
        public void ExceptionEvent(object sender, Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
 
        public void ProgressEvent(object sender, ProgressInfo progressInfo)
        {
            string message = $"{progressInfo.Progress}% | {progressInfo.OperationStatusText} | {progressInfo.SearchStatus} | {progressInfo.MatchedFileStatus}";
            Console.WriteLine(message);
        }
 
    }
}
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

FileSearchLogic Class
FileSearchLogic Members