tags:

views:

60

answers:

3

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.

+2  A: 

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
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
+2  A: 

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
There are a lot of gotchas when using FileSystemWatcher. I try to avoid it whenever possible.
PaulG
Hmmm yeah...a whole thread on the issue. http://stackoverflow.com/questions/239988/filesystemwatcher-vs-polling-to-watch-for-changes
Jeremy
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
A: 

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