Skip to content

Instantly share code, notes, and snippets.

@johnallers
Last active November 15, 2018 21:09
Show Gist options
  • Save johnallers/91edeca84c96bba3ed551a2f51a3b568 to your computer and use it in GitHub Desktop.
Save johnallers/91edeca84c96bba3ed551a2f51a3b568 to your computer and use it in GitHub Desktop.
Watch temp folder and make a copy of created files
class Program
{
static void Main()
{
const string COPIED_EXTENSION = ".copy";
var cts = new CancellationTokenSource();
Console.CancelKeyPress += (sender, args) =>
{
cts.Cancel();
};
var fsw = new FileSystemWatcher(@"C:\Users\John Allers\AppData\Local\Temp");
fsw.EnableRaisingEvents = true;
fsw.Created += (sender, args) =>
{
try
{
Console.WriteLine();
Console.WriteLine("Attempting to copy: " + args.FullPath);
if (!args.FullPath.EndsWith(COPIED_EXTENSION))
{
File.Copy(args.FullPath, args.FullPath + COPIED_EXTENSION);
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to copy: " + ex.ToString());
}
};
cts.Token.WaitHandle.WaitOne();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment