using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Diagnostics;
usingSystem.Linq;
usingSystem.ServiceProcess;
usingSystem.Text;
using System.IO;
usingSystem.Data.OleDb;
usingSystem.Reflection;
usingSystem.Data.Odbc;
usingMySql.Data.MySqlClient;
usingSystem.Configuration;
namespaceWindowsService
{
public partial class Service1 : ServiceBase
{
StreamWriter str;
FileStream fs = null;
FileSystemWatcher fsw = new FileSystemWatcher(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["FileWatchKey"]));
publicMySql.Data.MySqlClient.MySqlConnection con;
public Service1()
{
InitializeComponent();
}
protected override voidOnStart(string[] args)
{
fsw.Filter = "*.*";
fsw.EnableRaisingEvents = true;
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.Deleted += new FileSystemEventHandler(fsw_Deleted);
fsw.Renamed += new RenamedEventHandler(fsw_Renamed);
}
voidfsw_Renamed(Object sender, RenamedEventArgs e)
{
}
voidfsw_Deleted(Object sender, FileSystemEventArgs e)
{
}
voidfsw_Created(Object sender, FileSystemEventArgs e)
{
stringCreateLogfileLoc = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["CreateLogfileLoc"]) + "Log" + string.Concat(Convert.ToString(System.DateTime.Now.Day), "-", Convert.ToString(System.DateTime.Now.Month), "-", Convert.ToString(System.DateTime.Now.Year)) + ".txt";
try
{
if (!File.Exists(CreateLogfileLoc))
{
using (fs = File.Create(CreateLogfileLoc))
{
}
}
}
catch (Exception ex)
{
}
try
{
string[] varFile = e.Name.Split('.');
string FileName = Convert.ToString(System.Configuration.ConfigurationManager.
AppSettings["FileWatchKey"]) + e.Name;
if ((e.Name == "Report.xls") || (e.Name == "Report.xlsx"))
{
str.WriteLine("A new file " + e.Name + " has been created in " + fsw.Path);
Microsoft.Office.Interop.Excel.Workbook theWorkbook;
Microsoft.Office.Interop.Excel.Worksheet worksheet;
Microsoft.Office.Interop.Excel.Application ExcelObj = null;
Microsoft.Office.Interop.Excel.Range _range;
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
theWorkbook = ExcelObj.Workbooks._Open(FileName, 0, false, 5,
"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", true, true, 0, true);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
_range = worksheet.UsedRange;
int colCount = _range.Columns.Count;
int rowCount = _range.Rows.Count;
System.Array myvalues = (System.Array)_range.Cells.Value2;
DataTable dt = new DataTable();
if(myvalues.Length > 0)
{
stringstrConnctivity = string.Empty;
if (rowCount > 1)
{
dt.Rows.Clear();
dt.Columns.Clear();
DataColumn dc1 = new DataColumn("Name");
dt.Columns.Add(dc1);
for (int j = 2; j <= rowCount; j++)
{
try
{
dt.Rows.Add(CheckString(myvalues.GetValue(j, 12)));
}
catch (Exception ex)
{
}
}
}
}
ExcelObj.Workbooks.Close();
ExcelObj.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelObj);
GC.Collect();
GC.WaitForPendingFinalizers();
ExcelObj = null;
str.WriteLine("Report.xls data upload successfully");
}
}
catch (Exception ex)
{
str.WriteLine("Error in : " + e.Name + " .Error Code is:" + ex.Message.ToString());
}
finally
{
str.Flush();
}
}
public string CheckString(objectstr)
{
stringvarconvertValue = Convert.ToString(str);
if(varconvertValue == "NULL")
{
varconvertValue = "0";
}
else if (varconvertValue == "")
{
varconvertValue = "0";
}
returnvarconvertValue;
}
}
}