Thursday, 20 August 2015

How to create an advanced file searcher in C# using the FileSelectionManager library

This is how to use the pseudo-SQL features contained in the FileSelectionManager library for selecting and sorting files.
In order to do this, your application needs to receive a directory where it will search the files and pseudo-SQL sentence with the search criteria.

First of all you have to download the FileSelectionManager library, then create a console application and add the library to your project as a new reference

Next, write the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using FileSelectionManager;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            if(args.Count()!=2 || !Directory.Exists(args[0]))
                throw new ArgumentException("Invalid Parameters");
            else
            {
                //Selecting Files
                FSM fManager = new FSM();
                fManager.Dir(args[0], true, args[1]);

                //Show results
                Console.WriteLine("Files Affected: " + fManager.AffectedFiles);
                Console.WriteLine("Files Involved: " + fManager.InvolvedFiles);

                foreach (System.IO.FileInfo file in fManager.SelectedFiles)
                {
                    Console.WriteLine("Found: {0} - {1} - {2} - {3} ",
                    file.DirectoryName,
                    file.Name,
                    file.CreationTime,
                    file.Length);
                }
            }
        }
    }
}

Imagine you have the following directory and files structure:





























Compile your application and execute it, and give the directory the search criteria.
Here are three examples:














No comments:

Post a comment