tags:

views:

89

answers:

4

i know this code dont work but is describes good what im trying to do, i want to run the code inside the if check when Lastwritetime is greater then oldvalue date.

private void timer1_Tick(object sender, EventArgs e)
{
    DateTime lastWriteTime = File.GetLastWriteTime(@"C:\temp\test_folder\TestFile.txt");

    if (lastWriteTime.ToString() > oldValue.ToString())
    {
        MessageBox.Show("Succsess");
    }
    string oldValue = lastWriteTime.ToString();
}

edit: im not using SystemfileWatcher because of the mulitple events raised on change.

+12  A: 

Try a FileSystemWatcher instead.

Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

JeffH
just be aware that it occasionally misses a file update AND it sends lots of noise based on how users actually use files - create/rename/open/close/rename again/etc.
No Refunds No Returns
@NRNR : you can turn off all those other features
Ian
I have already tryed that approach but im getting some issues with muliple raised events on the change event that i dont know how to workaround.
Darkmage
JeffH
A: 

Check out the FileSystemWatcher class. It can monitor files and raises an event when they change.

scottm
+3  A: 

Why are you comparing string representations? If oldValue is DateTime as well, just compare them as is:

if(lastWriteTime.ToString() > oldValue)
    // ...

Plus, make sure that the scope of oldValue is greater than the scope of timer1_Tick (that is, ensure that it's a class member variable).

And of course, do not reinvent the wheel: FileSystemWatcher.

Anton Gogolev
ah ok is it now, put that inn while trying to bug track, ill fix it
Darkmage
+1  A: 

Consider the FileSystemWatcher class. It works pretty well.

One thing to be wary of, however, is that there can be issues with these when trying to monitor files that are network mounted, at least there were in .NET 2.0. We found that they could fail if the network connection got interrupted for some amount of time.

itsmatt