private void MonitorFileForChanges()
{
Timer timerFileMonitor = new Timer();
timerFileMonitor.Interval = 10000;
timerFileMonitor.Tick += timerFileMonitor_Tick;
timerFileMonitor.Start();
}
void timerFileMonitor_Tick(object sender, EventArgs e)
{
var directory = new DirectoryInfo(userVideosDirectory);
var myFile = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
long oldFileSize = 0;
}
My problem is how to check for the file size changes in the Tick event ? And when there is no more changes stop the timer.
Tried to use FileSystemWather:
private void WatchDirectory()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = userVideosDirectory;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size;
watcher.Filter = "*.mp4";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Changed)
{
var info = new FileInfo(e.FullPath);
var theSize = info.Length;
label2.BeginInvoke((Action)(() =>
{
label2.Text = theSize.ToString();
}));
}
dirchanged = true;
}
This is working but how do i check the last size change i mean how do i check it again after 5 or 10 seconds to see if there was another change and if not then stop the event ?
I mean there is no completed event like.