0
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.

Ben Naim
  • 17
  • 7
  • 2
    You might be interested in [`FileSystemWatcher`](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) – Blorgbeard Jul 29 '15 at 23:24

2 Answers2

1

You can use FileInfo.Length to get the size of the file in bytes. Something like:

FileInfo f = new FileInfo(pathOfTheFile);
long size = f.Length;
if (this.lastSize == size)
{
     timerFileMonitor.Stop();
}
else 
{
     this.lastSize = size;
}

You can stop the timer once the size of the file hasn't change since the last time. Just keep a field with the size and compare it.

Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
1

First of all, if you do not have a strong reason - do not use timer. It's a bad practice in most cases. Consider using FileSystemWatcher instead.

Regarding the file size - use FileInfo class for that.

  • Vitaliy i'm using now filesystemwatcher but how do i check when it finish update the file size change how do i know it ? There is no a completed event or something. I mean in the OnChanged event i have a flag that i set to true. This flag is good for checking if a new file created. So then i know in my program in another place that a new file created. But how do i know in my program somewhere else that the size is not changing any more ? I added to my question the way i'm using the FilesystemWatcher. – Ben Naim Jul 30 '15 at 00:17
  • As far as I understand you watch for video file being processed by external process. I think the best solution for you is to check whether the file is locked or not. Here is an [example](http://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use) how to check it. You can check everytime filesystemwatcher raises change event and most likely on the last change file will be unlocked - that's what you are waiting for. – Vitaliy Nesterenko Jul 30 '15 at 00:31
  • you right about a video file being processes by external process. I will true the example in the link. – Ben Naim Jul 30 '15 at 01:09
  • Working great. Thank you. – Ben Naim Jul 30 '15 at 01:20