Created
June 7, 2023 11:54
-
-
Save lakpahana/334f0da76729436768f3d8ac04302fad to your computer and use it in GitHub Desktop.
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.IO; | |
namespace ConsoleApp4 | |
{ | |
class MyClassCS | |
{ | |
static void Main() | |
{ | |
using var watcher = new FileSystemWatcher(@"C:\xampp\htdocs\FileSystemWatcher"); | |
watcher.NotifyFilter = NotifyFilters.Attributes | |
| NotifyFilters.CreationTime | |
| NotifyFilters.DirectoryName | |
| NotifyFilters.FileName | |
| NotifyFilters.LastAccess | |
| NotifyFilters.LastWrite | |
| NotifyFilters.Security | |
| NotifyFilters.Size; | |
watcher.Changed += OnChanged; | |
watcher.Created += OnCreated; | |
watcher.Deleted += OnDeleted; | |
watcher.Renamed += OnRenamed; | |
watcher.Error += OnError; | |
//watcher.Filter = "*.txt"; | |
watcher.IncludeSubdirectories = true; | |
watcher.EnableRaisingEvents = true; | |
Console.WriteLine("Press enter to exit."); | |
Console.ReadLine(); | |
} | |
private static void OnChanged(object sender, FileSystemEventArgs e) | |
{ | |
if (e.ChangeType != WatcherChangeTypes.Changed) | |
{ | |
return; | |
} | |
Console.WriteLine($"Changed: {e.FullPath}"); | |
} | |
private static void OnCreated(object sender, FileSystemEventArgs e) | |
{ | |
string value = $"Created: {e.FullPath}"; | |
Console.WriteLine(value); | |
} | |
private static void OnDeleted(object sender, FileSystemEventArgs e) => | |
Console.WriteLine($"Deleted: {e.FullPath}"); | |
private static void OnRenamed(object sender, RenamedEventArgs e) | |
{ | |
Console.WriteLine($"Renamed:"); | |
Console.WriteLine($" Old: {e.OldFullPath}"); | |
Console.WriteLine($" New: {e.FullPath}"); | |
} | |
private static void OnError(object sender, ErrorEventArgs e) => | |
PrintException(e.GetException()); | |
private static void PrintException(Exception? ex) | |
{ | |
if (ex != null) | |
{ | |
Console.WriteLine($"Message: {ex.Message}"); | |
Console.WriteLine("Stacktrace:"); | |
Console.WriteLine(ex.StackTrace); | |
Console.WriteLine(); | |
PrintException(ex.InnerException); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment