0

The problem now is that it's showing the file size as changed when creating it. It's true that the file has changed from 0 to some size but i want it to display the creation time and the file size and then only if the file has changed again only then display the changed size in the Fsw_Changed 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);
            }
        }

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

This method generate 100 png images :

private void GenerateImages()
        {
            Random rand = new Random();

            for (int I = 0; I < 100; I++)
            {
                Bitmap B = new Bitmap(rand.Next(1, 500), rand.Next(1, 500));
                Graphics G = Graphics.FromImage(B);
                G.DrawString(I.ToString(), this.Font, Brushes.Black, new PointF(100.0f, 100.0f));
                B.Save(@"d:\Images\" + I + ".png");
                B.Dispose();
            }
        }

        private void btnGenerateFiles_Click(object sender, EventArgs e)
        {
            GenerateImages();
        }
  • The event args has a ChangeType property that will allow you to determine if the firing is because of creation or modification? Latch onto Created, and remember some stuff about the file, so you can show this history you want when fsw fires because it was modified..? – Caius Jard Apr 26 '22 at 11:32
  • You may want to peruse this SO question… [FileSystemWatcher Changed event is raised twice](https://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice) … It is known that the `FileSystemWatcher.Created` event may fire other events like its OnChange event… [FileSystemWatcher.Created Event](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.created?view=net-6.0)... In my small test using your code… the Changed event did indeed fire when the file was created. – JohnG Apr 26 '22 at 19:57
  • @JohnG I noticed that when i create a file manual for example a text file the changed event is not fired because the created text file is 0 bytes empty. but if i create for example a png image file and give it a size for example 300bytes or 20KB then the changed event will fire. I edited my question with a method i did for testing that create 100 png images files with random sizes and this make the changed event fire. – Sani Berkovic Apr 26 '22 at 20:33
  • @JohnG the problem is that when you create some images files they are having size they are not empty so the changed event fire. – Sani Berkovic Apr 26 '22 at 20:35
  • Can you [edit] your question to show exactly what you want the text box to contain. You are correct that if the file size is zero (0) when created, then the `Create` event is fired but the `Changed` event is not. Also, If the file size is greater than zero when created… then the `Changed` event will also fire (possibly more than once). Have you looked at any of the solutions provided in the SO question my previous comment linked to? I am just not 100% sure what you want in the text box. – JohnG Apr 26 '22 at 23:15

0 Answers0