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);
}
}
}
Option Infer On
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports KellermanSoftware.FileSearchLibrary
Imports NUnit.Framework
Namespace KellermanSoftware.FileSearchLibraryExample
<TestFixture>
Public Class SearchFilesAsyncExample
<Test>
Public Sub SearchFilesAsync()
'Setup the search parameters
Dim parms As 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
AddHandler parms.CompleteEvent, AddressOf Complete
AddHandler parms.FileFoundEvent, AddressOf FileFound
AddHandler parms.ExceptionEvent, AddressOf ExceptionEvent
AddHandler parms.ProgressEvent, AddressOf ProgressEvent
'Search with the parameters
Dim fileSearchLogic As New FileSearchLogic()
Dim myTask = fileSearchLogic.SearchFilesAsync(parms)
Task.WaitAll(myTask)
'Detach Events
RemoveHandler parms.CompleteEvent, AddressOf Complete
RemoveHandler parms.FileFoundEvent, AddressOf FileFound
RemoveHandler parms.ExceptionEvent, AddressOf ExceptionEvent
RemoveHandler parms.ProgressEvent, AddressOf ProgressEvent
End Sub
Public Sub Complete(ByVal sender As Object, ByVal files As List(Of String))
Console.WriteLine("Complete".PadRight(40,"-"c))
For Each file In files
Console.WriteLine(file)
Next file
End Sub
Public Sub FileFound(ByVal sender As Object, ByVal file As String)
Console.WriteLine("File Found: " & file)
End Sub
Public Sub ExceptionEvent(ByVal sender As Object, ByVal ex As Exception)
Console.WriteLine(ex.Message)
End Sub
Public Sub ProgressEvent(ByVal sender As Object, ByVal progressInfo As ProgressInfo)
Dim message As String = $"{progressInfo.Progress}% | {progressInfo.OperationStatusText} | {progressInfo.SearchStatus} | {progressInfo.MatchedFileStatus}"
Console.WriteLine(message)
End Sub
End Class
End Namespace