Getting file name using FileSystemWatcher and FileSystemEventArgs

Anonymous
2021-05-17T01:47:17.007+00:00

I have a problem as to monitoring a file updated in the directory while using FileSystemWatcher and FileSystemEventArgs.
The path's text of the file that is caught by FileSystemEventHandler becomes an unexpected format.
The code is as follow;

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace WatcherTest
{
    class Watcher
    {  
        public Watcher()
        {
            fileSystemWatcher.Filter = "";
            fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
            fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fileSystemWatcher.EnableRaisingEvents = true;
        }
        private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(@"C:\Users\ntsol\Documents\test1");

        public void OnChanged(object obj,FileSystemEventArgs eventArgs)
        {
            Console.WriteLine(eventArgs.FullPath);
            //file name is test.xlsx therefore "C:\Users\ntsol\Documents\test1\test.xlsx" is expected 
            //but "C:\Users\ntsol\Documents\test1\94F7AF00" is displayed  
        }
    }
}

As above,the the part of file name in the path that is displayed is unexpected format.
Any help on this would be appreciated.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-05-17T06:35:21.857+00:00

    Hi ntsol-5133,
    When you save an xlsx file, it writes the data to a temporary file and then "moves" it to the final directory, which may be triggered by a "rename" event.
    My test also proved this and please refer to the following code.

        FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(@"C:\Users\Desktop\excle");  
        fileSystemWatcher.Filter = "";          
        fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite |  
                        NotifyFilters.DirectoryName | NotifyFilters.FileName |  
                        NotifyFilters.Size;  
        fileSystemWatcher.Renamed += new RenamedEventHandler(OnRenamed);  
        fileSystemWatcher.EnableRaisingEvents = true;  
      
    private static void OnRenamed(object source, RenamedEventArgs e)  
    {  
        // Specify what is done when a file is renamed.  
        //Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);  
        Console.WriteLine("File:{0}", e.FullPath);  
    }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Sam of Simple Samples 5,531 Reputation points
    2021-05-17T02:11:48.147+00:00

    Using the following:

    class Program
    {
        static AutoResetEvent e = new AutoResetEvent(false);
    
        static void Main(string[] args)
        {
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(@"G:\Temporary");
            fileSystemWatcher.Filter = "";
            fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
            fileSystemWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fileSystemWatcher.EnableRaisingEvents = true;
            e.WaitOne();
        }
    
        static public void OnChanged(object obj, FileSystemEventArgs eventArgs)
        {
            Console.WriteLine(eventArgs.FullPath);
            e.Set();
        }
    
    }
    

    In my system when I change G:\Temporary\t.txt I get:

    G:\Temporary\t.txt
    

  2. MD 1 Reputation point
    2022-09-20T15:58:35.547+00:00

    The reason is no changed event triggered when excel save his document !
    He saves a new temporary file and he renames it to your xlsx filename.
    You must managed the "renamed" event

    0 comments No comments