0

I want to check if file is recently modified. If the file is modified then only do the following: (If the file is not modified then don't do anything.)

How can I achieve that?

    if (lastLine.Contains("SFTP") || lastLine.Contains("ERROR"))
    {
        if (listBox2.InvokeRequired)
        {
            listBox2.Invoke(new MethodInvoker(delegate
            {
                listBox2.Items.Add(lastLine);
            }

            ));
        }
    }

    Thread.Sleep(5000);
MindSwipe
  • 7,193
  • 24
  • 47
Pooh
  • 19
  • 1
  • 7
  • You might want to check out the [FileSystemWatcher](https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.7.2) class, you could watch your file and do something when it was edited/ changed – MindSwipe Feb 22 '19 at 08:34
  • 5
    `if (DateTime.Now - new System.IO.FileInfo(@"c:\myFile.txt").LastWriteTime <= TimeSpan.FromSeconds(10)) {...}` - if file has been written 10 seconds ago... – Dmitry Bychenko Feb 22 '19 at 08:35
  • 2
    You could use the `FileInfo` class to obtain the last modified date and check against that. It's not 100% accurate as it's possible for a file to be modified without changes to the date, although that normally doesn't happen – NotFound Feb 22 '19 at 08:35
  • Possible duplicate https://stackoverflow.com/questions/13014704/how-to-work-out-if-a-file-has-been-modified – Hans Kesting Feb 22 '19 at 12:30

0 Answers0