Last active
November 15, 2018 21:09
-
-
Save johnallers/91edeca84c96bba3ed551a2f51a3b568 to your computer and use it in GitHub Desktop.
Watch temp folder and make a copy of created files
This file contains 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
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