File Search Library
CreateIndexAsync Method
Example 



KellermanSoftware.FileSearchLibrary Namespace > IndexLogic Class : CreateIndexAsync Method
Search Parameters
Create an index asynchronously
Syntax
'Declaration
 
Public Function CreateIndexAsync( _
   ByVal searchParms As SearchParms _
) As Task(Of Index)
'Usage
 
Dim instance As IndexLogic
Dim searchParms As SearchParms
Dim value As Task(Of Index)
 
value = instance.CreateIndexAsync(searchParms)
public Task<Index> CreateIndexAsync( 
   SearchParms searchParms
)
public:
Task<Index^>^ CreateIndexAsync( 
   SearchParms^ searchParms
) 

Parameters

searchParms
Search Parameters

Return Value

A list of matched files
Remarks
When creating the index it is possible to index all known text files by simply not specifying the FilesToInclude or specify the FilesToInclude as *.* See the ValidExtensions property on the SearchParms for all the file types that can be indexed.
Example
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
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

IndexLogic Class
IndexLogic Members