/// <summary> /// Creates an instance of the <see cref="FileSystemWatcherImplementation"/> class for the supplied /// <paramref name="path"/> if the argument references an existing <see cref="File"/> or <see cref="Directory"/>. /// </summary> /// <param name="path"> /// The path of the <see cref="File"/> or <see cref="Directory"/> to watch for changes. /// Might throw an <see cref="ArgumentException"/> if the <paramref name="path"/> neither refers to an existing file, /// nor an existing directory. /// </param> /// <param name="fileWatcherEventHandler"> /// An implementation of the <see cref="IFileWatcherEventHandler"/> interface that will be set up /// to get notified for events of the newly created <see cref="FileSystemWatcherImplementation"/> instance. /// </param> /// <returns> /// A new instance of the <see cref="FileSystemWatcherImplementation"/> class if the supplied <paramref name="path"/> /// is valid, otherwise <code>null</code>. /// </returns> public FileSystemWatcherImplementation(string path, IFileSystemWatcherEventHandler fileWatcherEventHandler = null) { // Store a reference to the supplied implementation of the IFileSystemWatcherEventHandler interface internally this.FileSystemWatcherEventHandler = fileWatcherEventHandler; // Trim the supplied path or fall back to an empty string path = path?.Trim() ?? string.Empty; // Check if the supplied path refers to an existing file or an existing directory var isExistingFile = File.Exists(path); var isExistingDirectory = Directory.Exists(path); // Throw an exception if the supplied path neither references a file nor a folder if (!isExistingFile && !isExistingDirectory) { throw new ArgumentException($"The supplied path neither is a directory, nor a file: '{path}'.", nameof(path)); } // Set the FileSystemWatcherType accordingly this.FileSystemWatcherType = isExistingFile ? FileSystemWatcherType.File : FileSystemWatcherType.Directory; // Create a new FileSystemWatcher instance using the object initializer syntax, which is atomic by design this.fileSystemWatcher = new FileSystemWatcher() { Path = isExistingFile ? Directory.GetParent(path).FullName : path, Filter = isExistingFile ? Path.GetFileName(path) : null, NotifyFilter = NotifyFilters.LastWrite | (isExistingFile ? NotifyFilters.FileName : NotifyFilters.DirectoryName) }; }
/// <summary> /// Asynchronously creates a new implementation of the <see cref="IFileSystemWatcher"/> interface watching the supplied /// <paramref name="path"/> and invoking the callback methods of the supplied <paramref name="fileWatcherEventHandler"/> /// if a change has been detected. /// </summary> /// <param name="path"> /// The path of the file or directory to watch. /// </param> /// <param name="fileWatcherEventHandler"> /// An implementation of the <see cref="IFileSystemWatcherEventHandler"/> interface to get notified when changes occur. /// Might be <c>null</c> and can be set later on using property <see cref="IFileSystemWatcher.FileSystemWatcherEventHandler"/>. /// </param> /// <returns> /// A <see cref="Task"/> that can be <c>await</c>ed to asynchronously create a new implementation of the /// <see cref="IFileSystemWatcher"/> interface to watch the supplied <paramref name="path"/>. /// </returns> public static Task <IFileSystemWatcher> CreateInstanceAsync(string path, IFileSystemWatcherEventHandler fileWatcherEventHandler = null) => new Task <IFileSystemWatcher>(() => new FileSystemWatcherImplementation(path, fileWatcherEventHandler));
/// <summary> /// Synchronously creates a new implementation of the <see cref="IFileSystemWatcher"/> interface watching the supplied /// <paramref name="path"/> and invoking the callback methods of the supplied <paramref name="fileWatcherEventHandler"/> /// if a change has been detected. /// </summary> /// <param name="path"> /// The path of the file or directory to watch. /// </param> /// <param name="fileWatcherEventHandler"> /// An implementation of the <see cref="IFileSystemWatcherEventHandler"/> interface to get notified when changes occur. /// Might be <c>null</c> and can be set later on using property <see cref="IFileSystemWatcher.FileSystemWatcherEventHandler"/>. /// </param> /// <returns> /// A new implementation of the <see cref="IFileSystemWatcher"/> interface to watch the supplied <paramref name="path"/>. /// </returns> public static IFileSystemWatcher CreateInstance(string path, IFileSystemWatcherEventHandler fileWatcherEventHandler = null) => new FileSystemWatcherImplementation(path, fileWatcherEventHandler);