How to check for file size changes only after the file created using filesystemwatcher ?

Chocolade 516 Reputation points
2022-04-26T12:37:24.93+00:00

The problem is once a file is created it's displaying the file creation time in the Fsw_Created event but then also display that the file size has changed in the Fsw_Changed event.
I want somehow to make that if a file was created first time don't display any changes only after the file was created then if something changed in the file then display it in the Fsw_Changed event.

Maybe to remember some stuff in the Fsw_Created event for each file and then check for it in the Fsw_Changed event ?
I'm not sure how to solve it.

The created event :

private void Fsw_Created(object sender, FileSystemEventArgs e)
        {
            string time = DateTime.Now.ToString("h:mm:ss tt");
            if (!richTextBoxLogChanges.Text.Contains(e.FullPath))
            {
                AppendText(richTextBoxLogChanges, $"File Name : {e.Name} ", Color.Red);
                AppendText(richTextBoxLogChanges, $"Created At : {time}", Color.Yellow);
                AppendText(richTextBoxLogChanges, "\n", Color.LightGreen);
            }
        }

The changed event :

        private void Fsw_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            else
            {
                var info = new FileInfo(e.FullPath);
                var theSize = info.Length;
                var size = SizeSuffix(theSize);

                string FileN1 = "File Name : ";
                string FileN2 = info.Name;
                string FileN3 = " Size Changed : ";
                string FileN4 = size;

                if (!richTextBoxLogChanges.Text.Contains(FileN1 + FileN2 + FileN3 + FileN4))
                {
                    AppendText(richTextBoxLogChanges, FileN1, Color.Red);
                    AppendText(richTextBoxLogChanges, FileN2, Color.Yellow);
                    AppendText(richTextBoxLogChanges, FileN3, Color.Red);
                    AppendText(richTextBoxLogChanges, FileN4, Color.LightBlue);
                    AppendText(richTextBoxLogChanges, "\n", Color.LightGreen);
                }
            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
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,234 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-04-28T09:10:37.99+00:00

    @Chocolade , Welcome to Microsoft Q&A, I suggest that you could use Dictionary to store the path and file size.

    I make a code example and you could refer to it.

        FileSystemWatcher watcher = new FileSystemWatcher("E:\\Test");  
        List<string> filelist = new List<string>();  
        Dictionary<string, long> dic = new Dictionary<string, long>();  
        private void Form1_Load(object sender, EventArgs e)  
        {  
            watcher.NotifyFilter = NotifyFilters.Attributes  
                                | NotifyFilters.CreationTime  
                                | NotifyFilters.DirectoryName  
                                | NotifyFilters.FileName  
                                | NotifyFilters.LastAccess  
                                | NotifyFilters.LastWrite  
                                | NotifyFilters.Security  
                                | NotifyFilters.Size;  
            watcher.Changed += Watcher_Changed;  
            watcher.Created += Watcher_Created;  
            watcher.Renamed += Watcher_Renamed;  
         
            watcher.Filter = "*.txt";  
            watcher.IncludeSubdirectories = true;  
            watcher.EnableRaisingEvents = true;  
            watcher.SynchronizingObject = this;  
      
            filelist = Directory.GetFiles("E:\\Test", "*.txt").ToList();  
            foreach (var item in filelist)  
            {  
                FileInfo info = new FileInfo(item);  
                dic.Add(item, info.Length);  
            }  
             
        }  
      
        private void Watcher_Renamed(object sender, RenamedEventArgs e)  
        {  
            FileInfo info = new FileInfo(e.FullPath);  
            if(dic.ContainsKey(e.FullPath))  
            {  
                dic[e.FullPath] = info.Length;  
            }  
            else  
            {  
                dic.Add(e.FullPath, info.Length);  
            }  
      
              
        }  
      
        private void Watcher_Created(object sender, FileSystemEventArgs e)  
        {  
            string time = DateTime.Now.ToString("h:mm:ss tt");  
      
            if (!richTextBox1.Text.Contains(e.FullPath))  
            {  
                richTextBox1.AppendText($"File Name : {e.Name} ");  
                richTextBox1.AppendText($"Created At : {time}");  
                filelist.Add(e.FullPath);  
                FileInfo info = new FileInfo(e.FullPath);  
                if (dic.ContainsKey(e.FullPath))  
                {  
                    dic[e.FullPath] = info.Length;  
                }  
                else  
                {  
                    dic.Add(e.FullPath, info.Length);  
                }  
            }  
        }  
      
        private void Watcher_Changed(object sender, FileSystemEventArgs e)  
        {  
            if (e.ChangeType != WatcherChangeTypes.Changed)  
            {  
                return;  
            }  
            else  
            {  
                var info = new FileInfo(e.FullPath);  
                var newSize = info.Length;  
                var oldsize = dic[e.FullPath];  
      
                string FileN1 = "File Name : ";  
                string FileN2 = info.Name;  
                string FileN3 = " Size Changed : From ";  
                string FileN4 = oldsize.ToString();  
                string FileN5 = "To";  
                string FileN6 = newSize.ToString();  
                if (newSize!=oldsize)  
                {  
                    richTextBox1.AppendText(FileN1);  
                    richTextBox1.AppendText(FileN2);  
                    richTextBox1.AppendText(FileN3);  
                    richTextBox1.AppendText(FileN4);  
                    richTextBox1.AppendText(FileN5);  
                    richTextBox1.AppendText(FileN6);  
                    richTextBox1.AppendText("\n");  
                     
                }  
      
                if (dic.ContainsKey(e.FullPath))  
                {  
                    dic[e.FullPath] = info.Length;  
                }  
                else  
                {  
                    dic.Add(e.FullPath, info.Length);  
                }  
            }  
        }  
    

    Tested Result:

    197307-2.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful