namespace Common.Services.LaserImport { using System; using System.Configuration; using System.IO; using System.Threading; class Program { static readonly KeyValueConfigurationCollection appsettings = ConfigurationManager .OpenExeConfiguration(typeof(Program).Assembly.Location) .AppSettings .Settings; static readonly string sharedFolder = appsettings["SharedFolder"].Value; static readonly string localFolder = appsettings["LocalFolder"].Value; static readonly FileSystemWatcher serverWatcher = new FileSystemWatcher(sharedFolder); static readonly FileSystemWatcher clientWhatcher = new FileSystemWatcher(localFolder); static readonly ManualResetEventSlim awaiter = new ManualResetEventSlim(); static string fileToDelete; static void Main() { serverWatcher.Created += ServerWatcher_FileCreated; serverWatcher.NotifyFilter = NotifyFilters.FileName; serverWatcher.IncludeSubdirectories = false; serverWatcher.EnableRaisingEvents = true; clientWhatcher.Deleted += CleintWatcher_FileDeleted; clientWhatcher.NotifyFilter = NotifyFilters.FileName; clientWhatcher.IncludeSubdirectories = false; clientWhatcher.EnableRaisingEvents = true; WaithLaserReady(); } private static void WaithLaserReady() { var fg = ConsoleColor.DarkGray; Console.ForegroundColor = fg; while (true) { awaiter.Reset(); awaiter.Wait(); Console.ForegroundColor = ConsoleColor.White; Console.Write($"LASERUNG BESTÄTIGEN"); Console.ReadLine(); Console.ForegroundColor = fg; while (File.Exists(fileToDelete)) { try { File.Delete(fileToDelete); Console.WriteLine($"{fileToDelete} - deleted"); break; } catch (Exception e) { Console.WriteLine(e.Message); Thread.Sleep(100); } } Console.Clear(); } } /// /// Occures when file deleted on laser PC /// /// /// private static void CleintWatcher_FileDeleted(object _, FileSystemEventArgs args) { fileToDelete = Path.Combine(sharedFolder, args.Name); if (!File.Exists(fileToDelete)) { Console.WriteLine($"{fileToDelete} - dose not exists!"); fileToDelete = string.Empty; } else { awaiter.Set(); } } /// /// Occures when file created from the laserkennzeichnung /// /// /// private static void ServerWatcher_FileCreated(object _, FileSystemEventArgs args) { var sourceFilePath = args.FullPath; var destinationFilePath = Path.Combine(localFolder, args.Name); Thread.Sleep(1000); if (File.Exists(sourceFilePath)) { while (true) { try { File.Copy(sourceFilePath, destinationFilePath, true); break; } catch (Exception e) { Console.WriteLine(e.Message); Thread.Sleep(100); } } Console.WriteLine($"{sourceFilePath} copied to {destinationFilePath}"); } else { Console.WriteLine($"{sourceFilePath} - dose not exists!"); } } } }