Friday 28 August 2015

Sort a FileInfo collection in C# like a database using FileSelectionManager

Adding the FileSelectionManager library to a project means you can select and sort files using pseudo-SQL sentences, like a database. In this article we focus on the sort feature.

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 file structure:



Compile your application and execute it, giving it the directory, the select criteria and the sort clause.
Here are three examples:




No comments:

Post a Comment