1

I am delivering media contents to a client system (download from internet) and want to acknowledge if the media file was opened (and viewed) by the operator. I am using a file system watcher (C# 3.5) to check for the last access time of file (so whenever the media file is played I should get the event) and I send an acknowledgement.

I have enabled the last access time in registry of my Windows 7 machine here and rebooted my system. File system watcher fires events on directory opened but not on media play.

Here is my code :

private FileSystemWatcher fsWatcher = null;
private static Object objLock = new Object();

private void StartAccessWatcher(string _folderPath)
{
  fsWatcher = new FileSystemWatcher(_folderPath, "*.*");
  fsWatcher.Changed += new FileSystemEventHandler(fsWatcher_Changed);
  fsWatcher.IncludeSubdirectories = true;
  fsWatcher.NotifyFilter = NotifyFilters.LastAccess;
  fsWatcher.EnableRaisingEvents = true;
}

private void fsWatcher_Changed(object sender, FileSystemEventArgs e)
{
  lock (objLock)
  {
    fsWatcher.EnableRaisingEvents = false;
    DisplayMessage(string.Concat(e.ChangeType, "  ", e.FullPath));
    fsWatcher.EnableRaisingEvents = true;
  }
}

private void btn_StartWatcher_Click(object sender, EventArgs e)
{
  StartAccessWatcher(@"E:\Surewaves\Media-Store\MP1 Videos");
}

You may also download my code sample from here.

Please tell me how best can I achieve it ? I need to get the the last access time (when ever the video or audio or image or .swf file was played by any player) ?

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
AkshayM
  • 317
  • 6
  • 18

2 Answers2

2

As an update to this three year old question, the code given works correctly now* with videos played on the local machine.

The machine must have lastAccess successfully enabled with the following command

fsutil behavior set DisableLastAccess 0

as noted in the question.


*- at the time of this post, using Windows Server 2012 R2

  • Thanks alot :) I will try it out. For the concerned project I changed our logic to delivering encrypted files and on decryption requests I record the timestamp. – AkshayM Jun 05 '15 at 10:43
0

The FileSystemWatcher is used to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. In your case there is no change being made on media files. I would suggest try extending FileSystemWatcher class and create new events for media file reading and then fire those events.

Sal Zaki
  • 41
  • 4
  • Thanks. I can inherit from FileSystemWatcher class and create my own events and fire them. But how will I know if the last access time (read) of any file (say media files) has changed ? Should I check it using a timer and FileInfo (which would not be a good way as I will have to very frequently fire the timer and check for it) ? Please suggest. – AkshayM Feb 11 '12 at 15:55
  • I would suggest when reading files, in your case media change there ReadOnly file attribute e.g. set ReadOnly to true or false as shown below, `code`File.SetAttributes(filePath, FileAttributes.ReadOnly); – Sal Zaki Feb 11 '12 at 23:25