question

Chocolade-4229 avatar image
0 Votes"
Chocolade-4229 asked Chocolade-4229 commented

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

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);
                 }
             }
         }

dotnet-csharpwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Declare a variable, maybe a list, that is global to the two events. In the Fsw_Created event add the file name to the variable. In the Fsw_Changed event check if the file name exists. If the file name exists then do whatever logic you need to do and remove the file name from the list.

1 Vote 1 ·

Like this in the changed event ?

filesNames is List<string> i make instance to the List in the top.
In the created event i just add the e.FullPath.

 for (int i = 0; i < filesNames.Count; i++)
                 {
                     if (File.Exists(filesNames[i]))
                     {
                         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);
                         }
    
                         filesNames.RemoveAt(i);
                     }
                 }
0 Votes 0 ·

The way i did it in my last comment is not working. not a good way.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered Chocolade-4229 commented

@Chocolade-4229, 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.


2.gif (220.1 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry for the late comment. I was overseas.

It's working fine i just want to ask what should i do in the Renamed event if i want it to display the old(original) file name and to what it has changed the new name ?
I need to add to the richTextBox some how the original name and the changed to name.

Now it's like this :

 private void Fsw_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);
             }
         }


0 Votes 0 ·

This is the renamed event.

So far i could make it to display the renamed name to but i'm not sure how to display also the old(original name) like :

Renamed From Test1.txt To Text2.txt

And also not sure if i need to keep and use the other part with the dic :

 private void Fsw_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);
             }
    
             string FileN1 = "To : ";
             string FileN2 = e.Name;
             AppendText(richTextBoxLogChanges, FileN1, Color.Red);
             AppendText(richTextBoxLogChanges, FileN2, Color.Yellow);
                
    
         }
0 Votes 0 ·