I am writing program which reads data from csv file when file update. Now i got simple error as shown following. i.e OnChanged event fires two or more times i want to execute it one time when file update.
public void GetData()
{
try
{
//for (int i = 0; i >= 0; i++)
//{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = filepath;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.csv";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
//}
}
catch { }
}
private void OnChanged(object source, FileSystemEventArgs e)
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
line = new string[] { };
using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(fs))
{
line = sr.ReadLine().Split(',');
for (int p = 0; p < line.Length; p++)
{
b = Encoding.ASCII.GetBytes(line[p]);
networkStream.Write(b, 0, b.Length);
}
sr.Close();
sr.Dispose();
fs.Flush();
}
}
catch { }
}