D
Devhelpplease
Guest
I have a windows service running with a file system watcher to do a database import. It works great the first time (although it fires twice). But then it stops firing for additional files. the service runs fine but the files do not trigger processing.
someone said make the watcher instance variable but i don't see how?
Its all mess code because i have tried everything...
Code below..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
namespace PayrollUpdateService
{
public partial class Service1 : ServiceBase
{
//started yet?
//static bool transflag = false;
// static int trips = 0;
static string lastfile = "";
public Service1()
{
InitializeComponent();
WriteToFile("Payroll Update Service was initialized at " + DateTime.Now);
}
protected override void OnStart(string[] args)
{
WriteToFile("Payroll Update Service is started at " + DateTime.Now);
// FileProcesser fp = new FileProcesser(ConfigurationManager.AppSettings["FromPath"]);
FileProcesser fp = new FileProcesser(@"\\fileserver\Folders\IT\test");
fp.Watch();
}
protected override void OnStop()
{
WriteToFile("Payroll Update Service is stopped at " + DateTime.Now);
}
///////////////////////////////////////////////////////////////////////////////////
public static void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
///////////////////////////////////////////////////////////////////////////////// write to file end
////////////////////////////////////////////////////////////////////////////////////////
public class FileProcesser
{
FileSystemWatcher watcher;
string directoryToWatch;
public FileProcesser(string path)
{
this.watcher = new FileSystemWatcher();
this.directoryToWatch = path;
}
public void Watch()
{
watcher.Path = directoryToWatch;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
// if (lastfile = e.FullPath)
// { //only process once
// return;
//}
int timeout = 5000;
while (timeout > 0)
{
try {
File.ReadAllText(e.FullPath);
//all copied..jump out and process it.
WriteToFile("Copy finished, process file.." + DateTime.Now);
timeout = 0;
}
catch (IOException)
{
WriteToFile("waiting for copy to finish.." + DateTime.Now);
}
Thread.Sleep(1000);
timeout -= 100;
}//while
//File.Copy(e.FullPath, @"\\fileserver\Folders\IT\test\processing\"+ Path.GetFileName(e.FullPath), true);
//Create the csv file and parce it...
string csvname = DateTime.Now.Ticks+"_payroll.csv";
WriteToFile(csvname + " sanatize txt file... " + DateTime.Now);
string text = File.ReadAllText(e.FullPath);
text = text.Replace("\",", "\"|");
text = text.Replace("\"\"|", "null|");
File.WriteAllText(@"\\fileserver\Folders\IT\test\processing\" + csvname, text);
WriteToFile("csv parsed...." + DateTime.Now);
WriteToFile(e.FullPath + ".....delete file ...." + DateTime.Now);
//delete text file..no longer needed
//File.Delete(e.FullPath);
///////////////////////////////////////////////////////////////////////
// timeout = 5000;
// while (timeout > 0)
// {
// try
// {
// File.ReadAllText(e.FullPath);
// File.Delete(e.FullPath);
//all copied..jump out and process it.
// WriteToFile("deleting file.." + DateTime.Now);
// timeout = 0;
// }
// catch (IOException)
// {
// WriteToFile("waiting to delete file.." + DateTime.Now);
// }
// Thread.Sleep(1000);
// timeout -= 100;
// }//while
///////////////////////////////////////////////////////////////////////////////
// String name = Path.GetFileName(e.FullPath).ToString();
// WriteToFile(name + " File Changed " + DateTime.Now);
//Truncate the Table
// TruncateTable();
WriteToFile("Temp table truncate done... " + DateTime.Now);
//Parce the CSV
// DataTable ProcessedcsvData = GetDataTabletFromCSVFile(@"\\fileserver\Folders\IT\test\processing\"+ csvname);
WriteToFile("CSV processed... " + DateTime.Now);
//Bulk insert the Datatable
// InsertDataIntoSQLServerUsingSQLBulkCopy(ProcessedcsvData);
WriteToFile("Done! " + DateTime.Now);
//remember last file
// lastfile = e.FullPath;
FileProcesser fp = new FileProcesser(@"\\fileserver\Folders\IT\test");
fp.Watch();
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
String name = Path.GetFileName(e.FullPath).ToString();
WriteToFile(name + " New file payroll received! " + DateTime.Now);
//do not nitify when working
// watcher.EnableRaisingEvents = false;
// File.Copy(e.FullPath, ConfigurationManager.AppSettings["ToPath"] + "\\" + Path.GetFileName(e.FullPath), true);
// File.Delete(e.FullPath);
// if (transflag == false)
// {
//File.Copy(@"\\fileserver\Folders\IT\test\payroll (78).txt", @"\\fileserver\Folders\IT\test\processing\payroll (78).txt",true);
//transflag = true;
//Sanatize the textfile
// string text = File.ReadAllText(@"\\fileserver\Folders\IT\test\payroll (78).txt");
// text = text.Replace("\",", "\"|");
// text = text.Replace("\"\"|", "null|");
// File.WriteAllText(@"\\fileserver\Folders\IT\test\payroll.csv", text);
// WriteToFile("csv parsed" + DateTime.Now);
// }
}
}
/////////////////////////////////////////////////////////////////////////////////////file watcher end
//////////////////////////////////////////////////////////////////////////////////////////////
private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{ //Parce the CSV to a databale for insert
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "|" });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
return null;
}
return csvData;
}
////////////////////////////////////////////////////GetDataTabletFromCSVFile end
////////////////////////////////////////////////////////////////////////////////////
static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)
{//Insert the datatable into the database.
//Create Connection
var c = new SqlConnection();
c.ConnectionString = "Server = Donald;Database =hehehehe;User Id=sa;Password=blabla;";
c.Close();
using (c)
{
c.Open();
using (SqlBulkCopy s = new SqlBulkCopy(c))
{
s.DestinationTableName = "NewTempEtime";
foreach (var column in csvFileData.Columns)
s.ColumnMappings.Add(column.ToString(), column.ToString());
s.WriteToServer(csvFileData);
}
c.Close();
}
}
//////////////////////////////////////////////////////////////InsertDataIntoSQLServerUsingSQLBulkCopy end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
static void TruncateTable()
{//Truncate the table for the new insert of data
var c = new SqlConnection(); // Your Connection String here
c.ConnectionString = "Server = Donald;Database =heheheh;User Id=sa;Password=blabbla;";
c.Close();
SqlCommand command1 = new SqlCommand("Truncate Table NewTempEtime;", c);
try
{ //connect to the database
c.Close();
c.Open();
}
catch
{
//Could not connect to database.;
return;
}
//ok add the record
try
{
command1.ExecuteNonQuery();
}
catch
{
//failed to execute
c.Close();
return;
}
c.Close();
return;
}
//////////////////////////////////////////////////////////////////////////////////////TruncateTable end
}
}
Continue reading...
someone said make the watcher instance variable but i don't see how?
Its all mess code because i have tried everything...
Code below..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using Microsoft.VisualBasic.FileIO;
using System.Threading;
namespace PayrollUpdateService
{
public partial class Service1 : ServiceBase
{
//started yet?
//static bool transflag = false;
// static int trips = 0;
static string lastfile = "";
public Service1()
{
InitializeComponent();
WriteToFile("Payroll Update Service was initialized at " + DateTime.Now);
}
protected override void OnStart(string[] args)
{
WriteToFile("Payroll Update Service is started at " + DateTime.Now);
// FileProcesser fp = new FileProcesser(ConfigurationManager.AppSettings["FromPath"]);
FileProcesser fp = new FileProcesser(@"\\fileserver\Folders\IT\test");
fp.Watch();
}
protected override void OnStop()
{
WriteToFile("Payroll Update Service is stopped at " + DateTime.Now);
}
///////////////////////////////////////////////////////////////////////////////////
public static void WriteToFile(string Message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
if (!File.Exists(filepath))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(filepath))
{
sw.WriteLine(Message);
}
}
else
{
using (StreamWriter sw = File.AppendText(filepath))
{
sw.WriteLine(Message);
}
}
}
///////////////////////////////////////////////////////////////////////////////// write to file end
////////////////////////////////////////////////////////////////////////////////////////
public class FileProcesser
{
FileSystemWatcher watcher;
string directoryToWatch;
public FileProcesser(string path)
{
this.watcher = new FileSystemWatcher();
this.directoryToWatch = path;
}
public void Watch()
{
watcher.Path = directoryToWatch;
watcher.NotifyFilter = NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.FileName |
NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object sender, FileSystemEventArgs e)
{
// if (lastfile = e.FullPath)
// { //only process once
// return;
//}
int timeout = 5000;
while (timeout > 0)
{
try {
File.ReadAllText(e.FullPath);
//all copied..jump out and process it.
WriteToFile("Copy finished, process file.." + DateTime.Now);
timeout = 0;
}
catch (IOException)
{
WriteToFile("waiting for copy to finish.." + DateTime.Now);
}
Thread.Sleep(1000);
timeout -= 100;
}//while
//File.Copy(e.FullPath, @"\\fileserver\Folders\IT\test\processing\"+ Path.GetFileName(e.FullPath), true);
//Create the csv file and parce it...
string csvname = DateTime.Now.Ticks+"_payroll.csv";
WriteToFile(csvname + " sanatize txt file... " + DateTime.Now);
string text = File.ReadAllText(e.FullPath);
text = text.Replace("\",", "\"|");
text = text.Replace("\"\"|", "null|");
File.WriteAllText(@"\\fileserver\Folders\IT\test\processing\" + csvname, text);
WriteToFile("csv parsed...." + DateTime.Now);
WriteToFile(e.FullPath + ".....delete file ...." + DateTime.Now);
//delete text file..no longer needed
//File.Delete(e.FullPath);
///////////////////////////////////////////////////////////////////////
// timeout = 5000;
// while (timeout > 0)
// {
// try
// {
// File.ReadAllText(e.FullPath);
// File.Delete(e.FullPath);
//all copied..jump out and process it.
// WriteToFile("deleting file.." + DateTime.Now);
// timeout = 0;
// }
// catch (IOException)
// {
// WriteToFile("waiting to delete file.." + DateTime.Now);
// }
// Thread.Sleep(1000);
// timeout -= 100;
// }//while
///////////////////////////////////////////////////////////////////////////////
// String name = Path.GetFileName(e.FullPath).ToString();
// WriteToFile(name + " File Changed " + DateTime.Now);
//Truncate the Table
// TruncateTable();
WriteToFile("Temp table truncate done... " + DateTime.Now);
//Parce the CSV
// DataTable ProcessedcsvData = GetDataTabletFromCSVFile(@"\\fileserver\Folders\IT\test\processing\"+ csvname);
WriteToFile("CSV processed... " + DateTime.Now);
//Bulk insert the Datatable
// InsertDataIntoSQLServerUsingSQLBulkCopy(ProcessedcsvData);
WriteToFile("Done! " + DateTime.Now);
//remember last file
// lastfile = e.FullPath;
FileProcesser fp = new FileProcesser(@"\\fileserver\Folders\IT\test");
fp.Watch();
}
private void OnCreated(object sender, FileSystemEventArgs e)
{
String name = Path.GetFileName(e.FullPath).ToString();
WriteToFile(name + " New file payroll received! " + DateTime.Now);
//do not nitify when working
// watcher.EnableRaisingEvents = false;
// File.Copy(e.FullPath, ConfigurationManager.AppSettings["ToPath"] + "\\" + Path.GetFileName(e.FullPath), true);
// File.Delete(e.FullPath);
// if (transflag == false)
// {
//File.Copy(@"\\fileserver\Folders\IT\test\payroll (78).txt", @"\\fileserver\Folders\IT\test\processing\payroll (78).txt",true);
//transflag = true;
//Sanatize the textfile
// string text = File.ReadAllText(@"\\fileserver\Folders\IT\test\payroll (78).txt");
// text = text.Replace("\",", "\"|");
// text = text.Replace("\"\"|", "null|");
// File.WriteAllText(@"\\fileserver\Folders\IT\test\payroll.csv", text);
// WriteToFile("csv parsed" + DateTime.Now);
// }
}
}
/////////////////////////////////////////////////////////////////////////////////////file watcher end
//////////////////////////////////////////////////////////////////////////////////////////////
private static DataTable GetDataTabletFromCSVFile(string csv_file_path)
{ //Parce the CSV to a databale for insert
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "|" });
csvReader.HasFieldsEnclosedInQuotes = true;
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
//Making empty value as null
for (int i = 0; i < fieldData.Length; i++)
{
if (fieldData == "")
{
fieldData = null;
}
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
return null;
}
return csvData;
}
////////////////////////////////////////////////////GetDataTabletFromCSVFile end
////////////////////////////////////////////////////////////////////////////////////
static void InsertDataIntoSQLServerUsingSQLBulkCopy(DataTable csvFileData)
{//Insert the datatable into the database.
//Create Connection
var c = new SqlConnection();
c.ConnectionString = "Server = Donald;Database =hehehehe;User Id=sa;Password=blabla;";
c.Close();
using (c)
{
c.Open();
using (SqlBulkCopy s = new SqlBulkCopy(c))
{
s.DestinationTableName = "NewTempEtime";
foreach (var column in csvFileData.Columns)
s.ColumnMappings.Add(column.ToString(), column.ToString());
s.WriteToServer(csvFileData);
}
c.Close();
}
}
//////////////////////////////////////////////////////////////InsertDataIntoSQLServerUsingSQLBulkCopy end
///////////////////////////////////////////////////////////////////////////////////////////////////////////
static void TruncateTable()
{//Truncate the table for the new insert of data
var c = new SqlConnection(); // Your Connection String here
c.ConnectionString = "Server = Donald;Database =heheheh;User Id=sa;Password=blabbla;";
c.Close();
SqlCommand command1 = new SqlCommand("Truncate Table NewTempEtime;", c);
try
{ //connect to the database
c.Close();
c.Open();
}
catch
{
//Could not connect to database.;
return;
}
//ok add the record
try
{
command1.ExecuteNonQuery();
}
catch
{
//failed to execute
c.Close();
return;
}
c.Close();
return;
}
//////////////////////////////////////////////////////////////////////////////////////TruncateTable end
}
}
Continue reading...