コード例 #1
0
        private void OnFileChanged(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            switch (e.ChangeType)
            {
            case WatcherChangeTypes.Changed:
                // [BIB]:  https://stackoverflow.com/a/3042963
                var modTime = File.GetLastWriteTime(TailedFilePath);
                if (modTime != LastModified)
                {
                    LastModified = modTime;
                    TailedFileChangeType temp = TailedFileChangeType.NoContentChange;
                    int linesRead             = lsLinesInFile.Count;
                    var currentSize           = new FileInfo(TailedFilePath).Length;
                    if (currentSize > StreamSize)
                    {
                        if (TailToEOF(out bool lastLineExtended))
                        {
                            linesRead = lsLinesInFile.Count - linesRead;
                            if (lastLineExtended)
                            {
                                temp = TailedFileChangeType.LastLineExtended;
                            }
                            else if (linesRead > 0)
                            {
                                temp = TailedFileChangeType.LinesAdded;
                            }
                            else
                            {
                                temp = TailedFileChangeType.Shrunk;
                            }
                        }
                    }
                    else
                    {
                        /**
                         * Treat both reduced file size and and unaltered file size as shrunk because we're not sure where
                         * the change has happened within the file; Hence a complete reset makes more sense.
                         */
                        temp = TailedFileChangeType.Shrunk;
                    }
                    if (temp == TailedFileChangeType.Shrunk)
                    {
                        fileStream.Close();
                        InitTailing();
                    }
                    // [BIB]:  https://stackoverflow.com/questions/33233161/how-to-ensure-that-streamreader-basestream-length-will-return-the-correct-value
                    // [BIB]:  https://stackoverflow.com/a/20863065
                    StreamSize = currentSize;
                    OnTailedFileChanged(this, new TailedFileChangedEventArgs(temp, linesRead));
                }
                break;

            case WatcherChangeTypes.Deleted:
                fileStream.Close();
                InitTailing();
                OnTailedFileChanged(this, new TailedFileChangedEventArgs(TailedFileChangeType.Deleted));
                break;
            }
        }
コード例 #2
0
 private void OnFileChanged(object source, FileSystemEventArgs e)
 {
     Console.WriteLine($"File: {e.FullPath} {e.ChangeType}");
     switch (e.ChangeType)
     {
     case WatcherChangeTypes.Changed:
         // [BIB]:  https://stackoverflow.com/a/3042963
         var modTime = File.GetLastWriteTime(FilePath);
         if (modTime != LastModified)
         {
             LastModified = modTime;
             TailedFileChangeType temp = TailedFileChangeType.NoContentChange;
             int linesRead             = LineCount;
             var currentSize           = new FileInfo(FilePath).Length;
             if (currentSize > FileSize)
             {
                 ReadLinesToEOF();
             }
             else
             {
                 // Treat both reduced file size and unaltered file size as shrunk
                 // because the the file may've changed other than at tail.
                 temp = TailedFileChangeType.Shrunk;
             }
             if (temp == TailedFileChangeType.Shrunk)
             {
                 ReInitTailing();
             }
             // [BIB]:  https://stackoverflow.com/questions/33233161/how-to-ensure-that-streamreader-basestream-length-will-return-the-correct-value
             // [BIB]:  https://stackoverflow.com/a/20863065
             LastKnownSize = currentSize;
             TailedFileChangedEventHandler(this, new TailedFileChangedEventArgs(temp, linesRead));
         }
         break;
     }
 }
コード例 #3
0
 // [BIB]:  https://www.codeproject.com/Articles/9355/Creating-advanced-C-custom-events
 #region Constructors
 /// <summary>
 /// Initializes a new instance of TailedFileChangedEventArgs
 /// </summary>
 /// <param name="changeType">Indicates the type of change that has occured.</param>
 /// <param name="changedLinesCount">Number of lines that have changed.</param>
 public TailedFileChangedEventArgs(TailedFileChangeType changeType, int changedLinesCount = 0)
 {
     ChangeType        = changeType;
     ChangedLinesCount = changedLinesCount;
 }