Click here to Skip to main content
15,395,181 members
Articles / Programming Languages / C#
Posted 13 Jan 2013


69 bookmarked

Smart Watcher

Rate me:
Please Sign up or sign in to vote.
5.00/5 (19 votes)
5 Sep 2013BSD5 min read
Easy to use directory watching tool.

Image 1


Note: now Smartwatcher is hosted by CodePlex , you can download the code and check updates on 

Smart Watcher is a simple windows service designed to watch a specific directories and taking specific actions to : Create - Change - Rename - Delete files events into those directories. The advantage of this tool:

  • Supporting plugins, just code about you business (what you want to do with file) compile your code into dll and let Smart Watcher take care about files watching.
  • Check if file is used by another service and wait before processing it (very useful specially when you are working with large files when coping or downloading them).
  • Support multiple file extensions and filters.
  • Using Regular Expressions for files filtering. - Watch many directories at the same time.
  • Creating a queue if many files created at the same time.
  • Archive the files into password protected zip files in case of success or failure.

Using the tool  

To use Smart Watcher you just need to install the service and develop your plugins then add them to the plugin folder into service root. To install the service just double click on the service executable file ( thank you to Sudheer Reddy Battula for his article : Installing .NET Windows Services (the easiest way) ) before starting the service you should set the SuccessArchivePath and FailureArchivePath settings into exe config file, to tell the service were you want to archive a zipped copy of files after processing. Also the 'ReadTries' into settings determine how many time the service should try to access a file in case in it using by another service, you should increase this value in case you are downloading large files. To create you plugin create a new class library project and inherit your class from the IPlugin interface and handle the interface properties and methods.

string Name { get; set; }
string WatchPath { get; set; }
string Filter { get; set; }
bool CatchAllFiles { get; set; }
string ZipPassword { get; set; }
string FilePrefix { get; set; }
IPluginHost Host { get; set; }
bool ArchiveFiles { get; set; }
bool DeleteFiles { get; set; }

/// <summary>
/// Event occurs when the a File or Directory is created
/// </summary>
/// <param name="fileName">The file full path name</param>
/// <returns></returns>
bool FileCreated(string fileName);
/// <summary>
/// Event occurs when the contents of a File or Directory are changed
/// </summary>
/// <param name="fileName">The file full path name</param>
/// <returns></returns>
bool FileChanged(string fileName);
/// <summary>
/// Event occurs when the a File or Directory is deleted
/// </summary>
/// <param name="fileName">The file full path name</param>
/// <returns></returns>
bool FileDeleted(string fileName);
/// <summary>
/// Event occurs when the a File or Directory is renamed
/// </summary>
/// <param name="fileName">The file full path name</param>
/// <returns></returns>
bool FileRenamed(string fileName); 

Name : the name of you plugin.
WatchPath : the path of the directory where you want the service to watch.
Filter: filter string used to determine what files are monitored in a directory.
ZipPassword : the password of the zip file created for archiving.
FilePrefix : a prefix added to the name of zipped files for archiving.
ArchiveFiles : if True Let the service to archive files by zipping and saving them into specific folders.
DeleteFiles : if True delete the file after finishing.
compile your library after that take the assembly file and change its extension from 'dll' to '.plug'
copy the assembly to the 'Plugin' folder into Smart Watcher bin folder
finally start the service.

Deeper Look  

By installing the service and writing your plugins you just scratch the surface, Let 's take deeper look about
how Smart Watcher is working, first of all let me redefine Smart Watcher for you with another words :
Smart Watch is a windows service which encapsulate FileSystemWatcher class, it is plug able service so you
can write your plugins and each plugin could be associated to watch a specific folder with specific file filter.
with every plugin you add, Smart Watcher automatically will create an FileSystemWatcher instance and add it to the service watchers list, The service will responsible about every thing else , it is include build in queue system to avoid any problem happen when many files created or changed at the same time, also it will check if file available and not used by another application before sending it to the appropriate plugin.


#region Variables
// Failur archive folder path from service setting
public static string SettingsFailureArchivePath = Properties.Settings.Default.FailureArchivePath;
// Success archive folder path from service setting
public static string SettingsSuccessArchivePath = Properties.Settings.Default.SuccessArchivePath;
// How many tries to access the file in case it used by another service
public static int SettingsReadTries = Properties.Settings.Default.ReadTries;
// the extention of zipped archived files
public static string SettingsZipFileExtention = "zip";
// the path of plugin folder (where the Smart Watcher will look for its plugins)
private readonly string _PluginFolder = AppDomain.CurrentDomain.BaseDirectory + @"Plugin\";

// a list of the avilable plugins into Plugin folder
public static IPlugin[] ipi;

// List of file watchers (the Smart Watcher create one for each plugin)
private List<FileSystemWatcher> _lstWatchers = new List<FileSystemWatcher>();

// FileProcessor is the class which maintains a queue of files to be processed and controls a worker thread which processes each file in the queue.
static FileProcessor processor;



#region Methods

// this function is responsible a about looking for plugins
// into Plugin folder and add them the Smart Wacher plugins list (ipi)
private void LoadPlugIns()

// After Smart Watcher detect its plugins it call this function to create
// a SystemFileWatcher for each plugin and initialize it with plugin sttings.
private void RegisterWatchers()



In the service constructor we first create a new instance of FileProcessor class to maintain files queue, then we call LoadPlugin method to check available plugins then we call RegisterWatchers function to create watchers list.

Note: the line System.Diagnostics.Debugger.Launch(); is to debug the service, for more details please check this URL

public SmartWatcherService()

    #if DEBUG

        // intilaize files processor to maintains files queue 
        processor = new FileProcessor();



There is four main file events into the service:

  • fswWatcher_Changed : Event occurs when the contents of a File or Directory are changed
  • fswWatcher_Created : Event occurs when the a File or Directory is created
  • fswWatcher_Deleted : Event occurs when the a File or Directory is deleted
  • fswWatcher_Renamed : Event occurs when the a File or Directory is renamed.

Each one of these events when fired calls the function processor.QueueInput(e.FullPath, e.ChangeType); where "e.FullPath" is the file name and e.ChangeType is the type of event , the QueueInput function add the file to a queue and it wait its turn for processing, when the file is ready two important functions are called .

private void ProcessFile(string fileInfo)
    string[] file = fileInfo.Split('|');
    WatcherChangeTypes changeType;
    WatcherChangeTypes.TryParse(file[0], true, out changeType);
    string filepath = file[1];

    if (!File.Exists(filepath))
        //ToDo: Add Handle Exception

    // Check if file is accessble
    if (!WaitReady(filepath, SmartWatcherService.SettingsReadTries))
        //ToDo: Add Log

    foreach (IPlugin t in SmartWatcherService.ipi)
        string strFiletr = t.Filter.Replace("*", @"\\*");
        Match match = Regex.Match(Path.GetFileName(filepath), strFiletr);
        if (match.Success == true)
            Func<string, bool> pluginFunc = null;
            switch (changeType)
                    case WatcherChangeTypes.Created:
                        pluginFunc = t.FileCreated;
                    case WatcherChangeTypes.Changed:
                        pluginFunc = t.FileChanged;
                    case WatcherChangeTypes.Renamed:
                        pluginFunc = t.FileRenamed;
                    case WatcherChangeTypes.Deleted:
                        pluginFunc = t.FileDeleted;

            ProcessPluginAction(pluginFunc, filepath, t.ArchiveFiles, t.DeleteFiles, t.FilePrefix,

the ProcessFile function look for which plugin has filter apply with this file using Regex which (so more than one plugin can process the file if the filter is apply) and for each apply it call the "ProcessPluginAction" function.

private void ProcessPluginAction(Func<string, bool> pluginMethod, string fileFullName, bool archiveFiles, bool deleteFiles, string filePrefix, string zipPassword)

    string strArchivePath = SmartWatcherService.SettingsFailureArchivePath;
        strArchivePath = SmartWatcherService.SettingsSuccessArchivePath;

    catch (Exception ex)
        //ToDo: add exception handeling                  
        if (archiveFiles == true)
            string strArchiveFileName = filePrefix + "_" + DateTime.Now.ToString("yyyyMMddHHmmssss") + "_" +
                                        Path.GetFileName(fileFullName) + "." + SmartWatcherService.SettingsZipFileExtention;
            ZipUtil.ZipFile(fileFullName, strArchivePath + strArchiveFileName, zipPassword);
        if (deleteFiles == true)
            if (File.Exists(fileFullName))
                // Check if file is accessble
                if (FileProcessor.WaitReady(fileFullName, SmartWatcherService.SettingsReadTries) == true)


The ProcessPluginAction function accept delegate Func parameter represent the method should be called from the plugin and the file name will pass to this method , also the other parameters work like flags for archiving (zipping) files or delete them after finish. 

Points of Interest  

Smart Watcher is an easy to use tool help you to automate a lot of tasks, it is able to be extended by developing many plugins to do various kinds of actions like : sending mails, printing documents, transferring files , Start programs , ..
I can compare it to some commercial solutions like :WatchDirectory
For files zipping I am using the open source library : SharpZipLib 


2013/1/13 Initial Release 0.1 

2013/08/06 Project added to CodePlex as SmartWatcher

2013/09/05 SmartWatcher has been released, You can check release notes and get its source code from here: SmartWatcher 

Release Notes:

- Add: Introducing SmartTimer: a feature which implements a timer that raises an event at user-defined intervals. Now SmartWatcher support the Tick event
which regularly invokes code. Every several seconds or minutes, Just handle this event in your plugin and SmartWatcher will invoke your code.

- Add: Introducing SmartActions: a library will include code snippets and function so you can use them directly into SmartWatcher plugins.

- Add: PluginsLibrary folder to SmartWatcher project to add future plugins. 






This article, along with any associated source code and files, is licensed under The BSD License


About the Author

Tammam Koujan
Software Developer (Senior)
Syrian Arab Republic Syrian Arab Republic

Comments and Discussions

QuestionWatching Network folders with Smart Watcher Pin
Member 1398354713-Sep-18 8:50
MemberMember 1398354713-Sep-18 8:50 
QuestionCould smartwatcher watch multiple LAN network directories? Pin
junzf8-Feb-17 2:12
Memberjunzf8-Feb-17 2:12 
AnswerRe: Could smartwatcher watch multiple LAN network directories? Pin
junzf8-Feb-17 2:42
Memberjunzf8-Feb-17 2:42 
AnswerRe: Could smartwatcher watch multiple LAN network directories? Pin
Tammam Koujan14-Feb-17 8:39
professionalTammam Koujan14-Feb-17 8:39 
GeneralRe: Could smartwatcher watch multiple LAN network directories? Pin
junzf17-Feb-17 15:56
Memberjunzf17-Feb-17 15:56 
QuestionMany file processing.. Pin
freakyit23-Jun-16 23:08
Memberfreakyit23-Jun-16 23:08 
AnswerRe: Many file processing.. Pin
Tammam Koujan24-Jun-16 6:01
professionalTammam Koujan24-Jun-16 6:01 
QuestionCannot find Pin
Member 1248261524-Apr-16 22:11
MemberMember 1248261524-Apr-16 22:11 
AnswerRe: Cannot find Pin
Tammam Koujan11-Jun-16 6:53
professionalTammam Koujan11-Jun-16 6:53 
GeneralMy vote of 5 Pin
D V L9-Mar-16 6:21
professionalD V L9-Mar-16 6:21 
General4d6520746f6f Pin
SteveHolle9-Jul-15 10:18
MemberSteveHolle9-Jul-15 10:18 
GeneralRe: 4d6520746f6f Pin
Tammam Koujan10-Jul-15 22:48
professionalTammam Koujan10-Jul-15 22:48 
GeneralThanks Pin
Claudio Barca7-May-14 21:56
MemberClaudio Barca7-May-14 21:56 
GeneralRe: Thanks Pin
Tammam Koujan8-May-14 2:11
professionalTammam Koujan8-May-14 2:11 
GeneralMy vote of 5 Pin
Tridip Bhattacharjee11-Sep-13 20:59
professionalTridip Bhattacharjee11-Sep-13 20:59 
GeneralRe: My vote of 5 Pin
Tammam Koujan12-Sep-13 1:24
professionalTammam Koujan12-Sep-13 1:24 
QuestionCan a program deny file delete & edit by file system watcher Pin
Tridip Bhattacharjee11-Sep-13 20:58
professionalTridip Bhattacharjee11-Sep-13 20:58 
AnswerRe: Can a program deny file delete & edit by file system watcher Pin
Tammam Koujan12-Sep-13 1:24
professionalTammam Koujan12-Sep-13 1:24 
QuestionNice job for file system watcher Pin
Tridip Bhattacharjee5-Sep-13 4:31
professionalTridip Bhattacharjee5-Sep-13 4:31 
AnswerRe: Nice job for file system watcher Pin
Tammam Koujan10-Sep-13 21:19
professionalTammam Koujan10-Sep-13 21:19 
GeneralMy vote of 5 Pin
fredatcodeproject5-Sep-13 1:17
professionalfredatcodeproject5-Sep-13 1:17 
GeneralRe: My vote of 5 Pin
Tammam Koujan5-Sep-13 1:18
professionalTammam Koujan5-Sep-13 1:18 
QuestionHow can I use it for multiple files ? Pin
Iliyan Kulishev23-Jun-13 8:23
MemberIliyan Kulishev23-Jun-13 8:23 
AnswerRe: How can I use it for multiple files ? Pin
Tammam Koujan23-Jun-13 23:51
professionalTammam Koujan23-Jun-13 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.