laatzen/Common/Common.Services.LaserImport/Program.cs
2026-01-23 11:16:38 +01:00

130 lines
4.1 KiB
C#

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();
}
}
/// <summary>
/// Occures when file deleted on laser PC
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
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();
}
}
/// <summary>
/// Occures when file created from the laserkennzeichnung
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
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!");
}
}
}
}