Created
August 17, 2017 19:08
-
-
Save anytizer/82d37616685832471d5990143bab8a8d to your computer and use it in GitHub Desktop.
File system watcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using api.general; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace console | |
{ | |
class run | |
{ | |
static void Main(string[] args) | |
{ | |
watcher w = new watcher(); | |
w.watch(@"D:\htdocs", "*.*", true); | |
//w.watch(@"D:\htdocs", "*.css", true); | |
// *.css watcher | |
// *.txt watcher | |
// *.log watcher | |
// *.html watcher | |
// *.php watcher | |
// @todo remove: Wait for the user to quit the program. | |
Console.WriteLine("Press \'q\' to quit the sample."); | |
while (Console.Read() != 'q') ; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Permissions; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace api.general | |
{ | |
/** | |
* @todo Watch directory changes | |
* @todo If directory was deleted, does not catch file names as deleted | |
*/ | |
public class watcher | |
{ | |
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] | |
[IODescription("FSW_IncludeSubdirectories")] | |
public void watch(string path = "/tmp", string filter = "*.txt", bool deepWatchSubdirectories = false) | |
{ | |
// Create a new FileSystemWatcher and set its properties. | |
FileSystemWatcher watcher = new FileSystemWatcher(); | |
watcher.IncludeSubdirectories = deepWatchSubdirectories; | |
watcher.Path = path; | |
/* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ | |
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; | |
// Only watch specified file extensions. | |
watcher.Filter = filter; | |
// Add event handlers. | |
watcher.Created += new FileSystemEventHandler(OnCreated); | |
watcher.Changed += new FileSystemEventHandler(OnChanged); | |
watcher.Renamed += new RenamedEventHandler(OnRenamed); | |
watcher.Deleted += new FileSystemEventHandler(OnDeleted); | |
// Begin watching. | |
watcher.EnableRaisingEvents = true; | |
} | |
// Define the event handlers. | |
private static void OnCreated(object source, FileSystemEventArgs e) | |
{ | |
// Specify what is done when a file is changed, created, or deleted. | |
Console.WriteLine("File Created: " + e.FullPath + " " + e.ChangeType); | |
} | |
private static void OnChanged(object source, FileSystemEventArgs e) | |
{ | |
// Specify what is done when a file is changed, created, or deleted. | |
Console.WriteLine("File Changed: " + e.FullPath + " " + e.ChangeType); | |
} | |
private static void OnRenamed(object source, RenamedEventArgs e) | |
{ | |
// Specify what is done when a file is renamed. | |
Console.WriteLine("File Renamed: {0} renamed to {1}", e.OldFullPath, e.FullPath); | |
} | |
private static void OnDeleted(object source, FileSystemEventArgs e) | |
{ | |
Console.WriteLine("File Deleted: " + e.FullPath + " " + e.ChangeType); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment