Skip to content

Instantly share code, notes, and snippets.

@ChrisPritchard
Created February 15, 2022 19:43
Show Gist options
  • Save ChrisPritchard/e27edaa8f8fcf3ad13dd26d1d5744a24 to your computer and use it in GitHub Desktop.
Save ChrisPritchard/e27edaa8f8fcf3ad13dd26d1d5744a24 to your computer and use it in GitHub Desktop.
A simple filewatcher used to preserve any files created and then renamed in a directory. Used for some save file shenanigans with Pillars of Eternity
var watcher = new FileSystemWatcher(".") { EnableRaisingEvents = true };
watcher.Renamed += (_, e) =>
{
if(Path.GetExtension(e.Name) != ".savegame")
{
Console.WriteLine($"Ignoring {Path.GetExtension(e.Name)} file");
return;
}
try
{
System.IO.File.Copy(e.FullPath, @"C:\Users\Christopher\Desktop\bak\" + e.Name);
Console.WriteLine("Copied " + e.Name);
}
catch (System.Exception ex)
{
Console.WriteLine("Failed to copy " + e.Name + ": " + ex.Message);
}
};
Console.WriteLine("Watching. Press any key to exit");
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment