FileSystemWatcher

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Kirk

    FileSystemWatcher

    Hi

    I am trying to use FileSystemWatch er to watch for changes in a directory.
    The problem I am exepriencing is a double firing of an OnChanged event when
    a file is opened, edited, and saved. Why is the event fired twice?

    Here is my code:

    static void Main(string[] args)
    {
    FileSystemWatch er watcher = new FileSystemWatch er();
    watcher.Path = @"g:\watchme ";
    watcher.Filter = "";
    //watcher.NotifyF ilter = NotifyFilters.F ileName | NotifyFilters.L astWrite;
    watcher.Changed += new FileSystemEvent Handler(OnChang ed);
    watcher.Created += new FileSystemEvent Handler(OnCreat ed);
    watcher.Deleted += new FileSystemEvent Handler(OnDelet ed);
    watcher.Renamed += new RenamedEventHan dler(OnRenamed) ;
    watcher.Include Subdirectories = false;
    watcher.EnableR aisingEvents = true;
    Console.Read();
    }
    private static void OnChanged(objec t source, FileSystemEvent Args e)
    {
    Console.WriteLi ne("OnChanged: FullPath=" + e.FullPath + "; Name=" +
    e.Name);
    }
    private static void OnCreated(objec t source, FileSystemEvent Args e)
    {
    Console.WriteLi ne("OnCreated: FullPath=" + e.FullPath + "; Name=" +
    e.Name);
    }
    private static void OnDeleted(objec t source, FileSystemEvent Args e)
    {
    Console.WriteLi ne("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
    e.Name);
    }
    private static void OnRenamed(objec t source, RenamedEventArg s e)
    {
    Console.WriteLi ne("OnRenamed: FullPath=" + e.FullPath + "; Name=" + e.Name
    + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
    }

    Thanks,
    Peter


  • Moty Michaely

    #2
    Re: FileSystemWatch er

    Hey Peter,

    What operation are you trying to do exactly on the file that fires those
    events?

    If you are trying just to save, LastWrite notify will fire as much times as
    the application saving the file writes and flushes the file.

    - Moty -

    "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
    news:uiNrWFkdFH A.228@TK2MSFTNG P12.phx.gbl...[color=blue]
    > Hi
    >
    > I am trying to use FileSystemWatch er to watch for changes in a directory.
    > The problem I am exepriencing is a double firing of an OnChanged event
    > when a file is opened, edited, and saved. Why is the event fired twice?
    >
    > Here is my code:
    >
    > static void Main(string[] args)
    > {
    > FileSystemWatch er watcher = new FileSystemWatch er();
    > watcher.Path = @"g:\watchme ";
    > watcher.Filter = "";
    > //watcher.NotifyF ilter = NotifyFilters.F ileName |
    > NotifyFilters.L astWrite;
    > watcher.Changed += new FileSystemEvent Handler(OnChang ed);
    > watcher.Created += new FileSystemEvent Handler(OnCreat ed);
    > watcher.Deleted += new FileSystemEvent Handler(OnDelet ed);
    > watcher.Renamed += new RenamedEventHan dler(OnRenamed) ;
    > watcher.Include Subdirectories = false;
    > watcher.EnableR aisingEvents = true;
    > Console.Read();
    > }
    > private static void OnChanged(objec t source, FileSystemEvent Args e)
    > {
    > Console.WriteLi ne("OnChanged: FullPath=" + e.FullPath + "; Name=" +
    > e.Name);
    > }
    > private static void OnCreated(objec t source, FileSystemEvent Args e)
    > {
    > Console.WriteLi ne("OnCreated: FullPath=" + e.FullPath + "; Name=" +
    > e.Name);
    > }
    > private static void OnDeleted(objec t source, FileSystemEvent Args e)
    > {
    > Console.WriteLi ne("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
    > e.Name);
    > }
    > private static void OnRenamed(objec t source, RenamedEventArg s e)
    > {
    > Console.WriteLi ne("OnRenamed: FullPath=" + e.FullPath + "; Name=" +
    > e.Name + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
    > }
    >
    > Thanks,
    > Peter
    >[/color]


    Comment

    • Peter Kirk

      #3
      Re: FileSystemWatch er

      I open the file in notepad (it's a simple text file), add a character, and
      save/close the file.

      Note that in the code I presented I have not set "watcher.Notify Filter" to
      anything. It didn't actually seem to make a difference to the results I saw.

      Peter

      "Moty Michaely" <moty@speedocs. co.il> skrev i en meddelelse
      news:%23LVG%232 kdFHA.2180@TK2M SFTNGP12.phx.gb l...[color=blue]
      > Hey Peter,
      >
      > What operation are you trying to do exactly on the file that fires those
      > events?
      >
      > If you are trying just to save, LastWrite notify will fire as much times
      > as the application saving the file writes and flushes the file.
      >
      > - Moty -
      >
      > "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
      > news:uiNrWFkdFH A.228@TK2MSFTNG P12.phx.gbl...[color=green]
      >> Hi
      >>
      >> I am trying to use FileSystemWatch er to watch for changes in a directory.
      >> The problem I am exepriencing is a double firing of an OnChanged event
      >> when a file is opened, edited, and saved. Why is the event fired twice?
      >>
      >> Here is my code:
      >>
      >> static void Main(string[] args)
      >> {
      >> FileSystemWatch er watcher = new FileSystemWatch er();
      >> watcher.Path = @"g:\watchme ";
      >> watcher.Filter = "";
      >> //watcher.NotifyF ilter = NotifyFilters.F ileName |
      >> NotifyFilters.L astWrite;
      >> watcher.Changed += new FileSystemEvent Handler(OnChang ed);
      >> watcher.Created += new FileSystemEvent Handler(OnCreat ed);
      >> watcher.Deleted += new FileSystemEvent Handler(OnDelet ed);
      >> watcher.Renamed += new RenamedEventHan dler(OnRenamed) ;
      >> watcher.Include Subdirectories = false;
      >> watcher.EnableR aisingEvents = true;
      >> Console.Read();
      >> }
      >> private static void OnChanged(objec t source, FileSystemEvent Args e)
      >> {
      >> Console.WriteLi ne("OnChanged: FullPath=" + e.FullPath + "; Name=" +
      >> e.Name);
      >> }
      >> private static void OnCreated(objec t source, FileSystemEvent Args e)
      >> {
      >> Console.WriteLi ne("OnCreated: FullPath=" + e.FullPath + "; Name=" +
      >> e.Name);
      >> }
      >> private static void OnDeleted(objec t source, FileSystemEvent Args e)
      >> {
      >> Console.WriteLi ne("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
      >> e.Name);
      >> }
      >> private static void OnRenamed(objec t source, RenamedEventArg s e)
      >> {
      >> Console.WriteLi ne("OnRenamed: FullPath=" + e.FullPath + "; Name=" +
      >> e.Name + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
      >> }
      >>
      >> Thanks,
      >> Peter
      >>[/color]
      >
      >[/color]


      Comment

      • Moty Michaely

        #4
        Re: FileSystemWatch er

        I missed that point :)

        Try to set the NotifyFilter to be NotifyFilters.L astWrite to watch for
        changes.

        If you want to watch for file rename events set the filters to be
        NotifyFilters.F ileName. etc.

        I am here for any further comments.
        -Moty-

        "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
        news:%23WLlM%23 kdFHA.3032@TK2M SFTNGP10.phx.gb l...[color=blue]
        >I open the file in notepad (it's a simple text file), add a character, and
        >save/close the file.
        >
        > Note that in the code I presented I have not set "watcher.Notify Filter" to
        > anything. It didn't actually seem to make a difference to the results I
        > saw.
        >
        > Peter
        >
        > "Moty Michaely" <moty@speedocs. co.il> skrev i en meddelelse
        > news:%23LVG%232 kdFHA.2180@TK2M SFTNGP12.phx.gb l...[color=green]
        >> Hey Peter,
        >>
        >> What operation are you trying to do exactly on the file that fires those
        >> events?
        >>
        >> If you are trying just to save, LastWrite notify will fire as much times
        >> as the application saving the file writes and flushes the file.
        >>
        >> - Moty -
        >>
        >> "Peter Kirk" <pk@alpha-solutions.dk> wrote in message
        >> news:uiNrWFkdFH A.228@TK2MSFTNG P12.phx.gbl...[color=darkred]
        >>> Hi
        >>>
        >>> I am trying to use FileSystemWatch er to watch for changes in a
        >>> directory.
        >>> The problem I am exepriencing is a double firing of an OnChanged event
        >>> when a file is opened, edited, and saved. Why is the event fired twice?
        >>>
        >>> Here is my code:
        >>>
        >>> static void Main(string[] args)
        >>> {
        >>> FileSystemWatch er watcher = new FileSystemWatch er();
        >>> watcher.Path = @"g:\watchme ";
        >>> watcher.Filter = "";
        >>> //watcher.NotifyF ilter = NotifyFilters.F ileName |
        >>> NotifyFilters.L astWrite;
        >>> watcher.Changed += new FileSystemEvent Handler(OnChang ed);
        >>> watcher.Created += new FileSystemEvent Handler(OnCreat ed);
        >>> watcher.Deleted += new FileSystemEvent Handler(OnDelet ed);
        >>> watcher.Renamed += new RenamedEventHan dler(OnRenamed) ;
        >>> watcher.Include Subdirectories = false;
        >>> watcher.EnableR aisingEvents = true;
        >>> Console.Read();
        >>> }
        >>> private static void OnChanged(objec t source, FileSystemEvent Args e)
        >>> {
        >>> Console.WriteLi ne("OnChanged: FullPath=" + e.FullPath + "; Name=" +
        >>> e.Name);
        >>> }
        >>> private static void OnCreated(objec t source, FileSystemEvent Args e)
        >>> {
        >>> Console.WriteLi ne("OnCreated: FullPath=" + e.FullPath + "; Name=" +
        >>> e.Name);
        >>> }
        >>> private static void OnDeleted(objec t source, FileSystemEvent Args e)
        >>> {
        >>> Console.WriteLi ne("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
        >>> e.Name);
        >>> }
        >>> private static void OnRenamed(objec t source, RenamedEventArg s e)
        >>> {
        >>> Console.WriteLi ne("OnRenamed: FullPath=" + e.FullPath + "; Name=" +
        >>> e.Name + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
        >>> }
        >>>
        >>> Thanks,
        >>> Peter
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...