Skip to content

Adding a New Search Condition to FileSharper

Andrew Vardeman edited this page Feb 17, 2018 · 6 revisions

Adding a new search condition to FileSharper is relatively simple. It typically only requires you to add two classes: one implementing ICondition and one with public properties for any parameters used by the condition. It's not necessary to add anything manually to the user interface; FileSharper uses reflection to build a user interface at runtime for any conditions it finds in the FileSharperCore assembly.

This tutorial will walk through the process of adding a "File Date" condition from scratch. (This condition already exists in the repository but will be recreated for purposes of explanation.) The condition will compare a file's created, modified, or accessed date to a date provided by the user to test whether the date is less than, greater than, or equal to the specified date.

Load FileSharper.sln in Visual Studio 2017 and Look Around

If you don't already have the FileSharper source, download or clone it from https://github.com/adv12/FileSharper. If you downloaded a zip, unzip it. Open the src/FileSharper directory and double-click FileSharper.sln to open it in Visual Studio. Solution Explorer should look something like this:

Initial Solution Explorer

FileSharper should be set as the startup project. The business logic is all in FileSharperCore. Expand FileSharperCore:

FileSharperCore Expanded

The main interfaces and base classes are in the root of FileSharperCore. Concrete implementations are organized under subdirectories. There are subdirectories for the four main types of "pluggable items": FileSources, Conditions, FieldSources, and Processors.

Add a New Condition Class

We will be implementing the ICondition interface by extending the ConditionBase abstract class. Concrete implementations of ICondition are organized under the Conditions directory. Because our condition tests a file system property, we will put it in the Filesystem subdirectory:

Conditions/Filesystem

Add a class by right-clicking Filesystem and selecting "Add->Class...". Name the class "FileDateCondition.cs". You should see something like this in Visual Studio:

New Condition Class

Extend ConditionBase

Make the class public if it isn't already, and make it extend ConditionBase:

Extending ConditionBase

(You can implement ICondition directly, but usually ConditionBase gives you a headstart.)

Visual Studio will now complain about unimplemented abstract members. Right-click the red squiggly and select "Implement Abstract Class." Your class will now look something like this:

ConditionBase Implemented

Set the Category, Name, and Description

Every condition should report its category, name, and description. The Category property is used to group similar conditions in the user interface. The Name property is the display name for the property in the user interface. The Description property is currently unused but will potentially be shown in the UI to give a longer description than that provided by Name.

For this condition, we'll set Category to "Filesystem," Name to "File Date", and Description to "File date compares to the specified date":

Category, Name, and Description

Create a Class for the Parameters

We want the user to be able to select which type of file date to look at (created, modified, or accessed), what date to compare to, and what comparison operator to use. Every ICondition has a public property of type object called Parameters. The user interface uses reflection to present the public properties of the Parameters object in a property editor. So getting user input for a condition is a simple matter of defining a class that contains the desired properties and returning it from the Parameters getter.

Rather than create a whole new file for the parameters, FileSharper code has a convention of defining a condition's related parameters class in the same file as the class that uses it. So add the following class as a sibling to the condition class in the file we've been editing:

public class FileDateComparisonParameters
{
    public FileDateType FileDateType { get; set; }
    public TimeComparisonType ComparisonType { get; set; }
    public DateTime Date { get; set; } = DateTime.Now;
    public string OutputFormat { get; set; } = "yyyy/MM/dd hh:mm:ss tt";
}

Then, add a member variable named m_Parameters of type FileDateComparisonParameters to the FileDateCondition class and update the Parameters property to return it:

private FileDateComparisonParameters m_Parameters = new FileDateComparisonParameters();
...
public override object Parameters => m_Parameters;
Clone this wiki locally