|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Permissions;
using System.Threading;
using System.Configuration;
using System.Collections.Specialized;
using System.Diagnostics;
namespace BatchProcess
{
class Program
{
private static string _strSourceDir;
private static string _strTargetDir;
private static string _strRejectDir;
private static volatile bool _blnStopProcessing;
private static ManualResetEvent _objSignal = new ManualResetEvent(false);
private static Thread WorkerThread;
private static EventLog Lg;
static void Main(string[] args)
{
// If Start or Stop is not specified, exit program.
if (args.Length != 1)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: BatchProcess.exe (START or STOP)");
return;
}
if (args[0] == "START")
Start();
else if (args[0] == "STOP")
Stop();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Start()
{
_blnStopProcessing = false;
string[] args = System.Environment.GetCommandLineArgs();
string lsource = ConfigurationSettings.AppSettings["SourceDirectory"];
string ltarget = ConfigurationSettings.AppSettings["TargetDirectory"];
string lreject = ConfigurationSettings.AppSettings["RejectDirectory"];
if (Directory.Exists(lsource))
{
_strSourceDir = lsource;
}
else
{
Console.WriteLine("Source directory not found");
return;
}
if (Directory.Exists(ltarget))
{
_strTargetDir = ltarget;
}
else
{
Console.WriteLine("Target directory not found");
return;
}
if (Directory.Exists(lreject))
{
_strRejectDir = lreject;
}
else
{
Console.WriteLine("Reject directory not found");
return;
}
//if (!EventLog.SourceExists("My Directory Monitor", "."))
// EventLog.CreateEventSource("My Directory Monitor", "Application");
//Lg = new EventLog("Application", ".", "My Directory Monitor");
WorkerThread = new Thread(NewThread);
WorkerThread.IsBackground = true;
WorkerThread.Start();
//the Main function loops until the worker thread object's IsAlive property gets set to true:
while (!WorkerThread.IsAlive) ;
Console.WriteLine("Press \'q\' to quit the sample.");
Thread.Sleep(10000);
while (Console.Read() != 'q') ;
}
static void NewThread()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = _strSourceDir;
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
Console.WriteLine("My Directory Monitor Started");
// Console.WriteLine("Press \'q\' to quit the sample.");
while (!_blnStopProcessing)
{
if (!_blnStopProcessing)
{
string[] lstr_arryFilesInDirectory = Directory.GetFiles(_strSourceDir);
// Begin watching.
if (lstr_arryFilesInDirectory.Length > 0)
{
watcher.EnableRaisingEvents = false;
ProcessFilesFound(lstr_arryFilesInDirectory);
}
else
{
watcher.EnableRaisingEvents = true;
_objSignal.WaitOne();
}
}
_objSignal.Reset();
}
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
_objSignal.Set();
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
_objSignal.Set();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
private static void Stop()
{
_blnStopProcessing = true;
_objSignal.Set();
Console.WriteLine("Batch process has been stopped");
}
private static void ProcessFilesFound(string[] lstr_arryFilesInDirectory)
{
try
{
Service1Client svcclient = new Service1Client();
Console.WriteLine("WCF service being called");
// Console.ReadLine();
var txtFiles = Directory.EnumerateFiles(_strSourceDir, "*.txt", SearchOption.AllDirectories);
foreach (string currentFile in txtFiles)
{
string fileName = currentFile.Substring(_strSourceDir.Length + 1);
string validate = fileName.Substring(0, 3);
string response = svcclient.ValidateService(validate);
if (response == "valid")
{
string logEntry = string.Format("File: {0} Created. Total time allocated {1} ms. Status {2}", fileName, DateTime.Now, response);
Console.WriteLine(logEntry);
Directory.Move(currentFile, Path.Combine(_strTargetDir, fileName));
}
else
{
string logEntry = string.Format("File: {0} Rejected. Total time allocated {1} ms. Error {2}", fileName, DateTime.Now, response);
Console.WriteLine(logEntry);
Directory.Move(currentFile, Path.Combine(_strRejectDir, fileName));
}
}
Console.WriteLine("Done" );
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am passionate on coding and application development specially in the .NET C# windows and web platform.