When I change file test.txt in notepad and save, my app executes "fw_Changed" two times.
I want it execute only one time.
Where is my mistake in my code?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
namespace DirAndFiles
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream("test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader sr = new StreamReader(fs);
FileSystemWatcher fw = new FileSystemWatcher(".", "test.txt");
fw.NotifyFilter = NotifyFilters.Size;
fw.Changed += new FileSystemEventHandler(fw_Changed);
fw.EnableRaisingEvents = true;
Console.ReadKey(false);
}
static void fw_Changed(object sender, FileSystemEventArgs e)
{
Console.WriteLine("----------");
Console.Write(e.ChangeType);
FileInfo fi = new FileInfo("test.txt");
Console.WriteLine(fi.Length.ToString());
}
}
}


Sign In
Create Account

Back to top









