|
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Diagnostics;
using System.Collections;
using System.IO;
using System.Timers;
namespace myService
{
internal class FileBackupService
{
internal EventLog LogService = new EventLog();
internal Timer ServiceTimer = new Timer();
internal ArrayList queueFile = new ArrayList();
internal FileSystemWatcher fsw = new FileSystemWatcher();
string serviceMode = "";
string targetFolder = "";
int fileNoProcess = 0;
internal void InitConfig()
{
serviceMode = ConfigurationManager.AppSettings["ServiceMode"];
targetFolder = ConfigurationManager.AppSettings["TargetFolder"];
fileNoProcess = int.Parse(ConfigurationManager.AppSettings["FileNoProcess"]);
// file monitor config
fsw.Filter = ConfigurationManager.AppSettings["FileFilter"];
fsw.Path = ConfigurationManager.AppSettings["FileMonitoringPath"];
fsw.EnableRaisingEvents = true;
fsw.NotifyFilter = ((System.IO.NotifyFilters)
((((System.IO.NotifyFilters.FileName
| System.IO.NotifyFilters.DirectoryName)
| System.IO.NotifyFilters.Size)
| System.IO.NotifyFilters.LastWrite)));
fsw.Created += new FileSystemEventHandler(fsw_Created);
// timer config
ServiceTimer.Interval = double.Parse(ConfigurationManager.AppSettings["ServiceTimer"]) * 1000;
ServiceTimer.Enabled = true;
ServiceTimer.Elapsed += new ElapsedEventHandler(ServiceTimer_Elapsed);
// event log config
LogService.Log = "Application";
LogService.Source = "FileBackupService";
}
private void ServiceTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ServiceTimer.Stop();
if (queueFile.Count > 0)
for (int i = 0; i < fileNoProcess; i++)
{
if (queueFile.Count > 0)
{
ProcessFileList(queueFile[0].ToString());
queueFile.RemoveAt(0);
}
else
{
break;
}
}
ServiceTimer.Start();
}
private void ProcessFileList(string filepath)
{
switch (serviceMode)
{
case "0":
FileProcess(filepath, targetFolder, FileServiceAction.COPY);
break;
case "1":
FileProcess(filepath, targetFolder, FileServiceAction.MOVE);
break;
case "2":
FileProcess(filepath, targetFolder, FileServiceAction.DELETE);
break;
}
}
private bool FileProcess(string pathFileTarget, string pathFileCopy, FileServiceAction action)
{
string filename = Path.GetFileName(pathFileTarget);
string taskMessage = "";
try
{
switch (action)
{
case FileServiceAction.COPY:
taskMessage = "Copy ";
File.Copy(pathFileTarget, pathFileCopy + filename);
break;
case FileServiceAction.MOVE:
taskMessage = "Move ";
File.Move(pathFileTarget, pathFileCopy + filename);
break;
case FileServiceAction.DELETE:
taskMessage = "Delete ";
File.Delete(pathFileTarget);
break;
}
WriteEventLog(taskMessage + pathFileTarget + " : Successful. ", EventLogEntryType.Information);
return true;
}
catch (Exception ex)
{
WriteEventLog(taskMessage + pathFileTarget + " : Error Message = " + ex.Message, EventLogEntryType.Error);
return false;
}
}
private void fsw_Created(object sender, FileSystemEventArgs e)
{
queueFile.Add(e.FullPath);
}
internal void WriteEventLog(string message, EventLogEntryType elet)
{
if (LogService != null)
{
LogService.WriteEntry(message, elet);
}
}
}
internal enum FileServiceAction
{
COPY,MOVE,DELETE
}
}
|