Jump to content

c# FileSystemWatcher Double Function Call

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Hooch

Hooch

    Newbie

  • Members
  • Pip
  • 6 posts
I have code.
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());

        }

    }

}


#2
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
It's probably because writing to file and closing the file both fire the Changed event.

You can try to distinguish these two by using some knowledge about the file. For example, if file is used for appending, then remember last file size and, if it didn't change, ignore the event.

#3
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
actually not, the change event triggers only when the content of the file or its attributes are changed. Your code should work perfectly but let me also cross check and report you back
i think the Wait for Change method could be used in conjunction with the Notify filter.
http://msdn.microsoft.com/en-us/library/67220zhk.aspx


#4
Hooch

Hooch

    Newbie

  • Members
  • Pip
  • 6 posts

gokuajmes said:

actually not, the change event triggers only when the content of the file or its attributes are changed. Your code should work perfectly but let me also cross check and report you back
i think the Wait for Change method could be used in conjunction with the Notify filter.
http://msdn.microsoft.com/en-us/library/67220zhk.aspx

Thanks.
I'm waiting for your response.

#5
Hooch

Hooch

    Newbie

  • Members
  • Pip
  • 6 posts
It works perfectly with log from game. But with file that I save in notepad it does it twice.

#6
zoranh

zoranh

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 207 posts
gokuajmes is right. Now I remember that I had the same behavior when using notepad for testing - Change event was fired twice every time. When I switched to other text editor, Change evet was fired only once. So I concluded it's notepad's problem, not mine. Try with different editor and tell what's the result.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users