using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using KellermanSoftware.FileSearchLibrary;
using NUnit.Framework;
namespace KellermanSoftware.FileSearchLibraryTests
{
[TestFixture]
public class IndexFilesAsyncExamples
{
[Test]
public void CreateIndexAsyncExample()
{
//Setup the search parameters
SearchParms searchParms = new SearchParms();
//This is the directory to index
searchParms.DirectoriesToSearch.Add(@"c:\_git");
//Include all C# files by wildcard
searchParms.FilesToInclude.Add("*.cs");
searchParms.FileIncludeSearchType = FileSearchType.Wildcard;
//Set the directory to save the index
searchParms.IndexPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FileSearchLibraryCreateIndexAsyncExample");
//Attach the events
searchParms.CompleteEvent += Complete;
searchParms.ExceptionEvent += ExceptionEvent;
searchParms.ProgressEvent += ProgressEvent;
//Create the index
IndexLogic indexLogic = new IndexLogic();
Task myTask = indexLogic.CreateIndexAsync(searchParms);
Task.WaitAll(myTask);
//Detach the events
searchParms.CompleteEvent -= Complete;
searchParms.ExceptionEvent -= ExceptionEvent;
searchParms.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 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.IO
Imports System.Threading.Tasks
Imports KellermanSoftware.FileSearchLibrary
Imports NUnit.Framework
Namespace KellermanSoftware.FileSearchLibraryTests
<TestFixture>
Public Class IndexFilesAsyncExamples
<Test>
Public Sub CreateIndexAsyncExample()
'Setup the search parameters
Dim searchParms As New SearchParms()
'This is the directory to index
searchParms.DirectoriesToSearch.Add("c:\_git")
'Include all C# files by wildcard
searchParms.FilesToInclude.Add("*.cs")
searchParms.FileIncludeSearchType = FileSearchType.Wildcard
'Set the directory to save the index
searchParms.IndexPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FileSearchLibraryCreateIndexAsyncExample")
'Attach the events
AddHandler searchParms.CompleteEvent, AddressOf Complete
AddHandler searchParms.ExceptionEvent, AddressOf ExceptionEvent
AddHandler searchParms.ProgressEvent, AddressOf ProgressEvent
'Create the index
Dim indexLogic As New IndexLogic()
Dim myTask As Task = indexLogic.CreateIndexAsync(searchParms)
Task.WaitAll(myTask)
'Detach the events
RemoveHandler searchParms.CompleteEvent, AddressOf Complete
RemoveHandler searchParms.ExceptionEvent, AddressOf ExceptionEvent
RemoveHandler searchParms.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 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