using System; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.IO; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; namespace AipGateway.FileJob.Scheduler { public partial class Service : ServiceBase { private const string settingDeleteMinMinute = "DeleteMinMinute"; private const string settingDelete = "deleteSettings"; private int DefaultDeleteMinute = 60 * 24; private string[] DeletePathsToWatchs = null; private int[] DeleteMinutes = null; private readonly ManualResetEvent stopEvent = null; private System.Timers.Timer deletePathTimer = null; public Service() { InitializeComponent(); loadConfiguration(); } private void loadConfiguration() { int deleteMinMinute = 30; NameValueCollection nvc = (NameValueCollection)ConfigurationManager.AppSettings; if (nvc.AllKeys.Contains(settingDeleteMinMinute)) { string tmpMinute = nvc[settingDeleteMinMinute]; deleteMinMinute = Helper.TryInt(tmpMinute, deleteMinMinute); if (deleteMinMinute < 30) { Console.WriteLine($"삭제할 파일 생성 시간 기준이 너무작아 [{deleteMinMinute}] 최소값인 {deleteMinMinute}분으로 다시 설정합니다."); deleteMinMinute = 30; } } try { var deleteSettings = ConfigurationManager.GetSection(settingDelete) as NameValueCollection; if (deleteSettings.Count == 0) { Console.WriteLine("등록되어 있는 작업 정보가 없습니다."); } else { int jobCnt = 0; string[] tmpDeletePathsToWatchs = new string[deleteSettings.Count]; int[] tmpDeleteMinutes = new int[deleteSettings.Count]; foreach (var key in deleteSettings.AllKeys) { tmpDeletePathsToWatchs[jobCnt] = key; tmpDeleteMinutes[jobCnt] = Helper.TryInt(deleteSettings[key], DefaultDeleteMinute); } DeletePathsToWatchs = tmpDeletePathsToWatchs; DeleteMinutes = tmpDeleteMinutes; } } catch (Exception ex) { Console.WriteLine(ex.ToString()); DeletePathsToWatchs = null; DeleteMinutes = null; } } protected override void OnStart(string[] args) { deletePathTimer = new System.Timers.Timer(); deletePathTimer.Interval = 60 * 1000; // 1시간마다 실행 deletePathTimer.Elapsed += Timer_Elapsed; deletePathTimer.Start(); serviceLoops(); } private void Timer_Elapsed(object sender, ElapsedEventArgs e) { try { serviceLoops(); } catch (Exception ex) { } } private void serviceLoops() { try { if (DeletePathsToWatchs == null || DeletePathsToWatchs.Length == 0) { Console.WriteLine("등록되어 있는 작업 정보가 없습니다. 설정정보를 다시 확인합니다."); loadConfiguration(); return; } for (int ii = 0; ii < DeletePathsToWatchs.Length; ii++) { Console.WriteLine($"Delete Watch Path: {DeletePathsToWatchs[ii]}"); DeletePathFiles(DeletePathsToWatchs[ii], DeleteMinutes[ii]); } } catch (Exception ex) { } } private void DeletePathFiles(string pathDir, int deleteMin) { if (System.IO.File.Exists(pathDir)) { Console.WriteLine($"'{pathDir}'는 파일입니다."); } else if (Directory.Exists(pathDir)) { Console.WriteLine($"'{pathDir}'는 디렉토리입니다."); // 디렉토리 내 모든 파일 목록 가져오기 (하위 디렉토리 포함) string[] fileNames = Directory.GetFiles(pathDir, "*", SearchOption.AllDirectories); foreach (string file in fileNames) { if (System.IO.File.Exists(file)) { DeleteFile(file, deleteMin); } } } else { Console.WriteLine($"'{pathDir}'는 존재하지 않는 경로입니다."); } } private void DeleteFile(string fileName, int deleteMin) { if (System.IO.File.Exists(fileName)) { Console.WriteLine($"'{fileName}'는 파일입니다."); FileInfo fileInfo = new FileInfo(fileName); if (fileInfo.CreationTime.Date < DateTime.Now.AddMinutes(-deleteMin)) { System.IO.File.Delete(fileName); Console.WriteLine($"'{fileName}'을 삭제했습니다."); } } } protected override void OnStop() { deletePathTimer.Stop(); stopService(); } private void stopService() { stopEvent?.Set(); Thread.Sleep(5000); } } }