1

I am programming something which involves certain files changing every second. Now, if the files are not changed for, say, 10 seconds it means that some error has occured somewhere, externally. So, I want the user to know about it. How can I implement this?

Sorry cant add comments due to some problem.

@Jeremy - Very small. Precisely, It wont exceed 10-15 characters.

Shubham
  • 21,300
  • 18
  • 66
  • 89
  • A roundabout way of doing it would be to have two variables that check file access times: one checks the most recently modified time (n), and the other holds the time before it (n-1), where n-1's initial value is the file creation time. Not very realistic in terms of programming though. I'd go with Jeremy's answer. – rownage Jul 30 '10 at 13:02
  • Any ideas on how large the file contents are? – Jeremy Jul 30 '10 at 15:32

3 Answers3

3

Check the LastWriteTime of the file:

If Date.Now.subtract(File.GetLastWriteTime("C:\yourPath\To\The\File.here")).TotalSeconds > 10 Then
    'do something'
End If

Edit: System.IO needs to be imported for this.

Bobby
  • 11,419
  • 5
  • 44
  • 69
  • Note from MSDN: This method may return an inaccurate value, because it uses native functions whose values may not be continuously updated by the operating system. – Jeremy Jul 30 '10 at 12:36
2

Investigate the FileSystemWatcher class. You should be able to keep track of the last time something changed, then use a timer to continuously check the difference in time.

Jeremy
  • 4,808
  • 2
  • 21
  • 24
  • There are a lot of gotchas when using FileSystemWatcher. I try to avoid it whenever possible. – PaulG Jul 30 '10 at 13:22
  • Hmmm yeah...a whole thread on the issue. http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-changes – Jeremy Jul 30 '10 at 15:31
  • Looks like @Shubham accepted this answer, so now we just have to wait for the 'why isnt it working' question :) I've used it once. Never again. (Edit) Ah, it didnt take long actually! – PaulG Jul 31 '10 at 14:28
1

Is it the file content that changes? If so you could hash the file content (or a selected snippet of it), then compare hash values every x seconds

Benefits of using this method is that you're not tied down by OS quirks (which FileSystemWatcher is full of) and wont have any issues if the file is updated twice in a second for example (which would be an issue if only monitoring the time the file changed).

PaulG
  • 13,871
  • 9
  • 56
  • 78