// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileWatcher.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// Available FileWatcher changes.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.IO;
namespace Catel.IO.Utils
{
/// <summary>
/// Available FileWatcher changes.
/// </summary>
public enum FileWatcherChangeType
{
/// <summary>
/// A file is added.
/// </summary>
FileAdded,
/// <summary>
/// A file is deleted.
/// </summary>
FileDeleted,
/// <summary>
/// A file is renamed.
/// </summary>
FileRenamed,
/// <summary>
/// A file is changed.
/// </summary>
FileChanged
}
/// <summary>
/// Arguments sent through when an event is fired
/// </summary>
public class FileWatcherEventArgs : EventArgs
{
#region Variables
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="FileWatcherEventArgs"/> class.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="path">The specific path.</param>
/// <param name="oldPath">The old path.</param>
/// <param name="oldName">The old name.</param>
/// <param name="changeType">Type of the change.</param>
public FileWatcherEventArgs(string fileName, string path, string oldPath, string oldName, FileWatcherChangeType changeType)
{
FileName = fileName;
Path = path;
OldFileName = oldName;
OldPath = oldPath;
ChangeType = changeType;
}
#endregion
#region Properties
/// <summary>
/// Gets the name of the file.
/// </summary>
/// <value>The name of the file.</value>
public string FileName { get; private set; }
/// <summary>
/// Gets the old name of the file.
/// </summary>
/// <value>The old name of the file.</value>
public string OldFileName { get; private set; }
/// <summary>
/// Gets the path.
/// </summary>
/// <value>The path to watch.</value>
public string Path { get; private set; }
/// <summary>
/// Gets the old path.
/// </summary>
/// <value>The old path.</value>
public string OldPath { get; private set; }
/// <summary>
/// Gets the type of the change.
/// </summary>
/// <value>The type of the change.</value>
public FileWatcherChangeType ChangeType { get; private set; }
#endregion
#region Methods
#endregion
}
/// <summary>
/// Monitors a folder for file system changes.
/// </summary>
public class FileWatcher
{
#region Variables
/// <summary>
/// The path to watch.
/// </summary>
private string _path;
/// <summary>
/// The filter for the files to watch.
/// </summary>
private string _filter;
/// <summary>
/// The actual <see cref="FileSystemWatcher"/> that is wrapped by this class.
/// </summary>
private FileSystemWatcher _fsw;
/// <summary>
/// A value indicating whether subdirectories are included or not.
/// </summary>
private bool _includeSubdirectories;
#endregion
#region Delegates
#endregion
#region Constructor & destructor
/// <summary>
/// Initializes a new instance of the <see cref="FileWatcher"/> class.
/// </summary>
public FileWatcher()
{
Path = ".";
Filter = "*.*";
CurrentFileName = string.Empty;
CurrentPath = string.Empty;
CurrentOldPath = string.Empty;
CurrentOldFileName = string.Empty;
IncludeSubdirectories = true;
CreateFileWatcher();
}
/// <summary>
/// Initializes a new instance of the <see cref="FileWatcher"/> class.
/// </summary>
/// <param name="path">The path to watch.</param>
/// <param name="filter">The filter.</param>
public FileWatcher(string path, string filter)
: this()
{
Path = path;
Filter = filter;
}
/// <summary>
/// Initializes a new instance of the <see cref="FileWatcher"/> class.
/// </summary>
/// <param name="path">The path to watch.</param>
public FileWatcher(string path)
: this()
{
Path = path;
}
#endregion
#region Events
/// <summary>
/// Occurs when something has changed.
/// </summary>
public event EventHandler<FileWatcherEventArgs> Changed = null;
#endregion
#region Properties
/// <summary>
/// Gets or sets the path to watch.
/// </summary>
/// <value>The path to watch.</value>
public string Path
{
get
{
return _path;
}
set
{
_path = value;
_fsw.Path = _path;
}
}
/// <summary>
/// Gets or sets the filter.
/// </summary>
/// <value>The filter.</value>
public string Filter
{
get
{
return _filter;
}
set
{
_filter = value;
_fsw.Filter = _filter;
}
}
/// <summary>
/// Gets a value indicating whether this instance is started.
/// </summary>
/// <value>
/// <c>true</c> if this instance is started; otherwise, <c>false</c>.
/// </value>
public bool IsStarted { get { return _fsw.EnableRaisingEvents; } }
/// <summary>
/// Gets the name of the current file.
/// </summary>
/// <value>The name of the current file.</value>
public string CurrentFileName { get; private set; }
/// <summary>
/// Gets the name of the current old file.
/// </summary>
/// <value>The name of the current old file.</value>
public string CurrentOldFileName { get; private set; }
/// <summary>
/// Gets the current path.
/// </summary>
/// <value>The current path.</value>
public string CurrentPath { get; private set; }
/// <summary>
/// Gets the current old path.
/// </summary>
/// <value>The current old path.</value>
public string CurrentOldPath { get; private set; }
/// <summary>
/// Gets the type of the current change.
/// </summary>
/// <value>The type of the current change.</value>
public FileWatcherChangeType CurrentChangeType { get; private set; }
/// <summary>
/// Gets or sets a value indicating whether to include sub directories or not.
/// </summary>
/// <value>
/// <c>true</c> if sub directories should be included; otherwise, <c>false</c>.
/// </value>
public bool IncludeSubdirectories
{
get
{
return _includeSubdirectories;
}
set
{
_includeSubdirectories = value;
if (_fsw != null)
{
_fsw.IncludeSubdirectories = _includeSubdirectories;
}
}
}
#endregion
#region public methods
/// <summary>
/// Start the watcher for a specific folder with a specific filter.
/// </summary>
public void StartWatcher()
{
if (string.IsNullOrEmpty(_path))
{
return;
}
_fsw.Path = _path;
_fsw.Filter = _filter;
_fsw.IncludeSubdirectories = _includeSubdirectories;
_fsw.EnableRaisingEvents = true;
}
/// <summary>
/// Stop the folder watcher from raising events.
/// </summary>
public void StopWatcher()
{
_fsw.EnableRaisingEvents = false;
}
/// <summary>
/// Creates the file watcher.
/// </summary>
private void CreateFileWatcher()
{
_fsw = new FileSystemWatcher(_path, _filter);
_fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName;
_fsw.Changed += OnChanged;
_fsw.Created += OnCreated;
_fsw.Deleted += OnDeleted;
_fsw.Renamed += OnRenamed;
}
/// <summary>
/// Raises the <see cref="E:Changed"/> event.
/// </summary>
/// <param name="e">The <see cref="Catel.IO.Utils.FileWatcherEventArgs"/> instance containing the event data.</param>
protected virtual void OnChanged(FileWatcherEventArgs e)
{
if (Changed != null)
{
Changed(this, e);
}
}
/// <summary>
/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private void OnCreated(object source, FileSystemEventArgs args)
{
CurrentFileName = args.Name;
CurrentPath = args.FullPath;
CurrentOldFileName = string.Empty;
CurrentOldPath = string.Empty;
CurrentChangeType = FileWatcherChangeType.FileAdded;
OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileAdded));
}
/// <summary>
/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.IO.RenamedEventArgs"/> instance containing the event data.</param>
private void OnRenamed(object source, RenamedEventArgs args)
{
CurrentFileName = args.Name;
CurrentPath = args.FullPath;
CurrentOldFileName = args.OldFullPath;
CurrentOldPath = args.OldName;
CurrentChangeType = FileWatcherChangeType.FileRenamed;
OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileRenamed));
}
/// <summary>
/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private void OnDeleted(object source, FileSystemEventArgs args)
{
CurrentFileName = args.Name;
CurrentPath = args.FullPath;
CurrentOldFileName = string.Empty;
CurrentOldPath = string.Empty;
CurrentChangeType = FileWatcherChangeType.FileDeleted;
OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileDeleted));
}
/// <summary>
/// Called when the <see cref="FileSystemWatcher"/> has detected a change.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="args">The <see cref="System.IO.FileSystemEventArgs"/> instance containing the event data.</param>
private void OnChanged(object source, FileSystemEventArgs args)
{
CurrentFileName = args.Name;
CurrentPath = args.FullPath;
CurrentOldFileName = string.Empty;
CurrentOldPath = string.Empty;
CurrentChangeType = FileWatcherChangeType.FileChanged;
OnChanged(new FileWatcherEventArgs(CurrentFileName, CurrentPath, CurrentOldPath, CurrentOldFileName, FileWatcherChangeType.FileChanged));
}
#endregion
}
}