FileSystemWatcher vs polling to watch for file changes

后端 未结 13 2297
一个人的身影
一个人的身影 2020-11-22 05:44

I need to setup an application that watches for files being created in a directory, both locally or on a network drive.

Would the FileSystemWatcher or p

相关标签:
13条回答
  • 2020-11-22 06:06

    I have run into trouble using FileSystemWatcher on network shares. If you're in a pure Windows environment, it might not be an issue, but I was watching an NFS share and since NFS is stateless, there was never a notification when the file I was watching changed.

    0 讨论(0)
  • 2020-11-22 06:08

    I currently use the FileSystemWatcher on an XML file being updated on average every 100 milliseconds.

    I have found that as long as the FileSystemWatcher is properly configured you should never have problems with local files.

    I have no experience on remote file watching and non-Windows shares.

    I would consider polling the file to be redundant and not worth the overhead unless you inherently distrust the FileSystemWatcher or have directly experienced the limitations everyone else here has listed (non-Windows shares, and remote file watching).

    0 讨论(0)
  • 2020-11-22 06:10

    Working solution for working with create event instead of change

    Even for copy, cut, paste, move.

    class Program
    {        
    
            static void Main(string[] args)
            {
                string SourceFolderPath = "D:\\SourcePath";
                string DestinationFolderPath = "D:\\DestinationPath";
                FileSystemWatcher FileSystemWatcher = new FileSystemWatcher();
                FileSystemWatcher.Path = SourceFolderPath;
                FileSystemWatcher.IncludeSubdirectories = false;
                FileSystemWatcher.NotifyFilter = NotifyFilters.FileName;   // ON FILE NAME FILTER       
                FileSystemWatcher.Filter = "*.txt";         
                 FileSystemWatcher.Created +=FileSystemWatcher_Created; // TRIGGERED ONLY FOR FILE GOT CREATED  BY COPY, CUT PASTE, MOVE  
                FileSystemWatcher.EnableRaisingEvents = true;
    
                Console.Read();
            }     
    
            static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
            {           
                    string SourceFolderPath = "D:\\SourcePath";
                    string DestinationFolderPath = "D:\\DestinationPath";
    
                    try
                    {
                        // DO SOMETING LIKE MOVE, COPY, ETC
                        File.Copy(e.FullPath, DestinationFolderPath + @"\" + e.Name);
                    }
                    catch
                    {
                    }          
            }
    }
    

    Solution for this file watcher while file attribute change event using static storage

    class Program
    {
        static string IsSameFile = string.Empty;  // USE STATIC FOR TRACKING
    
        static void Main(string[] args)
        {
             string SourceFolderPath = "D:\\SourcePath";
            string DestinationFolderPath = "D:\\DestinationPath";
            FileSystemWatcher FileSystemWatcher = new FileSystemWatcher();
            FileSystemWatcher.Path = SourceFolderPath;
            FileSystemWatcher.IncludeSubdirectories = false;
            FileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;          
            FileSystemWatcher.Filter = "*.txt";         
            FileSystemWatcher.Changed += FileSystemWatcher_Changed;
            FileSystemWatcher.EnableRaisingEvents = true;
    
            Console.Read();
        }     
    
        static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.Name == IsSameFile)  //SKIPS ON MULTIPLE TRIGGERS
            {
                return;
            }
            else
            {
                string SourceFolderPath = "D:\\SourcePath";
                string DestinationFolderPath = "D:\\DestinationPath";
    
                try
                {
                    // DO SOMETING LIKE MOVE, COPY, ETC
                    File.Copy(e.FullPath, DestinationFolderPath + @"\" + e.Name);
                }
                catch
                {
                }
            }
            IsSameFile = e.Name;
        }
    }
    

    This is a workaround solution for this problem of multiple triggering event.

    0 讨论(0)
  • 2020-11-22 06:12

    Returning from the event method as quickly as possible, using another thread, solved the problem for me:

    private void Watcher_Created(object sender, FileSystemEventArgs e)
    {
        Task.Run(() => MySubmit(e.FullPath));
    }
    
    0 讨论(0)
  • 2020-11-22 06:16

    Also note that file system watcher is not reliable on file shares. Particularly if the file share is hosted on a non-windows server. FSW should not be used for anything critical. Or should be used with an occasional poll to verify that it hasn't missed anything.

    0 讨论(0)
  • 2020-11-22 06:16

    I'd go with polling.

    Network issues cause the FileSystemWatcher to be unreliable (even when overloading the error event).

    0 讨论(0)
提交回复
热议问题