FileSystemWatcher is used to view a folder / file

I walked around, but I can not find any information about what I'm looking for, if there is another message that is already going through this, I apologize.

I am looking for help with code that will track a specific folder when a folder is opened by another person or when the file under the specified folder is opened. At this point, I see when the user opens and modifies any files, but if they just open the file to view it, it does not throw an event, even when I add LastAccessed. Any information or help would be appreciated.

Folder Name: C: \ Junk

Code in C # 4.0:

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")] public static void Run() { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = @"C:\"; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = "junk"; // Add event handlers. watcher.Changed += new FileSystemEventHandler(OnChanged); watcher.Created += new FileSystemEventHandler(OnChanged); watcher.Deleted += new FileSystemEventHandler(OnChanged); watcher.Renamed += new RenamedEventHandler(OnRenamed); watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; // Wait for the user to quit the program. Console.WriteLine("Press \'q\' to quit the sample."); while (Console.Read() != 'q') ; } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is changed, created, or deleted. Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); } private static void OnRenamed(object source, RenamedEventArgs e) { // Specify what is done when a file is renamed. Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath); } 
+6
source share
5 answers

it does not generate an event even when LastAccessed is added.

Because NotifyFilters.LastAccessed indicates that you want to find this property, not the event you want to subscribe to. Available events: Changed , Created or Deleted , none of which do what you want.

You should take a look at the ReadDirectoryChangesW documented Win32 function. It can be passed the FILE_NOTIFY_CHANGE_LAST_ACCESS flag, which seems to deliver what you want:

Any change in the last access time of files in the scanned directory or subtree results in the return of the change notification operation.

Edit: ignore this, FileSystemWatcher executes the NotifyFilters.LastWrite internal pass as int 32, which is the same as FILE_NOTIFY_CHANGE_LAST_ACCESS , with ReadDirectoryChangesW . This function still does not notify about access to the file, I tried.

Perhaps this is caused by this :

Last access time has free detail, which ensures that the time will be accurate within one hour. In Windows Vista, we turned off updates for recent access times to improve NTFS performance. If you use an application that relies on this value, you can enable it using the following command:

 fsutil behavior set disablelastaccess 0 

For this change to take effect, you must restart the computer.

If you do this on the command line, LastAccess may be written and the event will fire. I will not try to connect to my SSD and not have a virtual machine, but in Windows 7 disablelastaccess seems to be included out of the box.

If it still doesn’t work when you turn off this behavior, wait for Raymond Chen (or himself) to appear, as a rule, there is a quite logical explanation of why the documentation does not seem to correctly describe the behavior you encounter ;; -)

You can simply scan the directory in a loop and look at the LastAccessed property of the files. What are you trying to do when a user opens a specific file?

+2
source

You must install

 watcher.Path = @"C:\junk"; 

and delete the line watcher.Filter if the event should fire for all files

Using the Filter property, you can set wildcards to match files, for example *.txt

0
source

what you really need is an NtQuerySystemInformation enumeration and a timer, so you can scan the directory and see if any of the files are open. the file system will not give you this information

0
source
 public void OnChanged(object sender, FileSystemEventArgs e) { string FileName = System.IO.Path.GetFileName(e.FullPath); if(IsAvailable(System.IO.Path.Combine(RecievedPath,FileName))) { ProcessMessage(FileName); } } private void ProcessMessage(string fileName) { try { File.Copy(System.IO.Path.Combine(RecievedPath,fileName), System.IO.Path.Combine(SentPath,fileName)); MessageBox.Show("File Copied"); } catch (Exception) { } } private static bool IsAvailable(String filePath) { try { using (FileStream inputStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)) { if (inputStream.Length > 0) { return true; } else { return false; } } } catch (Exception) { return false; } } 
0
source

To get the path to the On-Access file, there is one solution for the minifilter driver. To fulfill the requirements, you must implement the minifilter driver.

0
source

All Articles