I have a FileSystemWatcher, that moves excel data to a database when ever an excel sheet is made. But it keeps closing on the second iteration, and I can't understand why. Can someone please help. Here is the code.
private void StartWatcher()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* 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 = "*.xls";
// Add event handler.
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
private 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);
StreamWriter te = new StreamWriter("fileList.txt");
te.WriteLine(e.FullPath.ToString());
te.Close();
CreateIncidents(mX, dX);
// txtOutput.AppendText(e.Name);
}
void CreateIncidents(IncidentsObjectsDataContext crt, IncidentsObjectsDataContext art)
{
string addr, crx;
StreamReader kt = new StreamReader("fileList.txt");
string xl = kt.ReadLine();
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(xl, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.ActiveSheet;
int index = 0;
int rowIndex = 18;
DateTime dt5 = DateTime.FromOADate(0.0);
string temp5;
while (((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2 != null)
{
temp5 = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2);
//Converts OADatetime to DateTime
addr = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 2]).Value2);
crx = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 3]).Value2);
try
{
Double rogue = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 5]).Value2;
dt5 = DateTime.FromOADate(Convert.ToDouble(rogue));
addr = addr.Replace("BLK", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty);
crx = crx.Replace("BLK", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty);
incident newInc = new incident();
newInc.Agency = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2);
newInc.Intersection = addr + "& " + crx;
newInc.CallTime = dt5;
newInc.IncidentType = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 6]).Value2);
dX.incidents.InsertOnSubmit(newInc);
dX.SubmitChanges();
index++;
rowIndex = 18 + index;
}
catch
{
try
{
Double rogue = ((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 6]).Value2;
dt5 = DateTime.FromOADate(Convert.ToDouble(rogue)); joinedResp newJR = new joinedResp();
newJR.Agency = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 1]).Value2);
newJR.Address = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 2]).Value2);
newJR.CrossStreet = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 3]).Value2);
newJR.IncidentType = Convert.ToString(((Microsoft.Office.Interop.Excel.Range)workSheet.Cells[rowIndex, 6]).Value2);
mX.joinedResps.InsertOnSubmit(newJR);
mX.SubmitChanges();
index++;
rowIndex = 18 + index;
}
catch
{
index++;
rowIndex = 18 + index;
}
}
}
// workBook = null;
// app = null;
// workBook.Close();
// app.Quit();
}
public void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fd = new FolderBrowserDialog();
fd.ShowDialog();
path = fd.SelectedPath;
StartWatcher();
}