using System; using System.Collections.Generic; using System.Text; using MySql.Data.MySqlClient; using System.IO; using System.Windows.Forms; using ZedGraph; using System.Drawing; namespace Bubbler { class Gases { public void CreateWatcher() { //Create a new FileSystemWatcher. FileSystemWatcher watcher = new FileSystemWatcher(); //Set the filter to only catch TXT files. watcher.Filter = "*.txt"; //Subscribe to the Created event. watcher.Created += new FileSystemEventHandler(watcher_FileCreated); //Set the path to C:\Temp\ watcher.Path = @"Y:\\GalaxieData\"; //Enable the FileSystemWatcher events. watcher.EnableRaisingEvents = true; } void watcher_FileCreated(object sender, FileSystemEventArgs e) { //A new .TXT file has been created in C:\Temp\ //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType); ReadGasFile(e.FullPath, e.Name); } public void ReadGasFile(string path, string filename) { try { //Create the Connection String string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=Y:\GalaxieData\; Extended Properties=Text"; //Create the connection System.Data.OleDb.OleDbConnection GasConnection = new System.Data.OleDb.OleDbConnection (ConnectionString); // Query string string GasQuery; //filename = "Export" + w + ".TXT"; GasQuery = "Select * from " + filename; System.Data.OleDb.OleDbCommand GasCommand = new System.Data.OleDb.OleDbCommand(GasQuery, GasConnection); //Connect GasConnection.Open(); System.Data.OleDb.OleDbDataReader GasReader; GasReader = GasCommand.ExecuteReader(); //Array to hold gas values double[] GasValues = new double[4]; // Read 3rd line from file for (int u = 1; u <= 3; u++) { GasReader.Read(); if (u == 3) { GasValues.SetValue(GasReader.GetDouble(2), 0); GasValues.SetValue(GasReader.GetDouble(3), 1); GasValues.SetValue(GasReader.GetDouble(4), 2); GasValues.SetValue(GasReader.GetDouble(5), 3); } } //Get Date and time of File Creation and convert it into Mysql DateTime System.IO.FileInfo fi = new System.IO.FileInfo("Y:\\GalaxieData\\" + filename); DateTime FileTime = new DateTime(); string FileName; FileName = "Y:\\GalaxieData\\" + filename; FileTime = fi.LastWriteTime; string Y = FileTime.Year.ToString(); string M = FileTime.Month.ToString(); string D = FileTime.Day.ToString(); string H = FileTime.Hour.ToString(); string m = FileTime.Minute.ToString(); string s = FileTime.Second.ToString(); string ft = Y + "-" + M + "-" + D + " " + H + ":" + m + ":" + s; //Close connection GasConnection.Close(); //Pass Gas Values, File Creation Time and Full Path to File name to //InsertGasPercentages InsertGasPercentages(GasValues, ft, FileName); } catch (Exception) { } } public void InsertGasPercentages(double[] GasValues, string filetime,string filename) { //Create Connection String string MyConString = "SERVER=localhost;" + "DATABASE=newtest;" + "UID=root;" + "PASSWORD=Anna2;"; //Query string insertinto = "Insert into Gases (Argon,SF6,C4H10,R134A,FileName,FileTime)values('" + GasValues[0] + "','" + GasValues[1] + "','" + GasValues[2] + "','" + GasValues[3] + "','" + filename + "','" + filetime + "')"; //Connect MySqlConnection connection = new MySqlConnection(MyConString); //Create Command Object MySqlCommand command = connection.CreateCommand(); //Create Reader Object MySqlDataReader Reader; //Command Text command.CommandText = insertinto; //Open connection connection.Open(); //Execute Command Reader = command.ExecuteReader(); //Close Connection connection.Close(); } } }