FileSystemWatcher Delete and Shift +Delete events are different - c#

FileSystemWatcher C#
Folder Structure C:\A\B\C\D\E\F\abc.txt
Delete a nested folder B fires only one event for the root folder B
But Shift + Delete fires the events for all sub folders and files.
I need to get Delete nested folder events same as Shift + Delete
The below question is similar to my requirement, but it's not working
C# FileSystemWatcher.Deleted not Firing on "normal" deleting?

The below question is similar to my requirement, but it's not working
Define "not working". As mentioned in the accepted answer for question that you referenced, the default behavior in Windows is that when you press the delete key, the file is not actually deleted- it is moved to a special folder called the recycle bin.
If you handle the renamed and changed events, you should see evidence of the moves to the recycle bin, but as you found the events will probably be raised only for the parent folder (see also Detecting moved files using FileSystemWatcher). There is probably no way to get the events for the child folders/files. If you need to keep track of the children, you'll need to index them into some data structure before they get deleted.
Also, keep in mind that some people (like me) disable the recycle bin altogether, and for us a delete is a delete.

Related

Moving and cut folders INSIDE watched folder C#

I am using a FileSystemWatcher and i got 2 cases that don't raise events.
lets say i watch on C:/temp,
In case i have already 2 folders with files inside the watched directory, if i cut-paste or move them inside the watched dir to another folder i dont get any event.
Some one know a way i can get events on this files that moved?
Watched directory:
c:/temp
|--test1
| |--test1.txt
|
|--test2
| |--test2.txt
if i move or cut-paste test2 folder into test1 i don't get event on test2.txt.
EDIT: I'm using the code from FileSystemWatcher docs which can find here:
https://learn.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=net-5.0
hope you can help me, thanks :)
Moving a folder or a file doesn't change it. If you want to track moves, make sure to watch for the Renamed event and set the filters appropriately.
As per the documentation:
COPYING AND MOVING FOLDERS
Event Handler
Events Handled
Performs
OnChanged
Changed, Created, Deleted
Report changes in file attributes, created files, and deleted files
OnRenamed
Renamed
List the old and new paths of renamed files and folders, expanding recursively if needed.
Note that strictly speaking, the file system watcher doesn't watch for changes in content - only the filesystem entries. It's possible to change file contents without changing the filesystem entries, so make sure that it's good enough for your use.

C# FileWatcher - How to Include only one subfolder?

I told my FileWatcher, that it should include Subdirectories
_watcher.IncludeSubdirectories = true;
The problem, that I have is, that there are multiple files with the same file-ending.
So my watcher gets triggered multiple times.
I have something like this
(subfolder[x] is the watched one)
mainFolder/subfolder[x]/ -> in this one example.mkv and two other filetypes
mainFolder/subfolder[x]/subfolder2/ -> in this one is just one sample.mkv
FileWatcher is configured to:
_watcher.Filter = "*.mkv";
Currently the "created" event gets triggered two times ("example.mkv" and "sample.mkv")
I would like to have it just triggered once for the "example.mkv" and not for the "sample.mkv".
How can I just "watch" the first subfolder and exclude the second subfolder?
You want to only Watch the target and 1st Order Child Directories/first two levels of the directory tree. Close to 0 code that is able to include subdirectories recusively on a Windows ever had such an ability. It was either "incldue all subdirectories" or "include no subdirectories".
Only code as modern as Robocopy has any ability to define the "depth". And FileWatcher is not nearly that modern. It is (t)rusty like hte Office COM Interop. It is still following the old "all Subdirectories or None" pattern. So you still have to use the old workaround - manual recursion:
Do not use IncludeSubdirectories = true;
Start a normal, non recursive File watcher on the root directory
Itterate over all subdirectories manually. The Directory class should have you covered
Start a seperate, non-recursive watcher for each subdirectory
Unfortuantely FileWatcher is not the most advanced class. For example figuring out wich kind of changed happened is nigh impossible. Wich is why someone took 9 watches with Filters and put them into one class, to get some decent level of information out of this class: https://www.codeproject.com/Articles/58740/FileSystemWatcher-Pure-Chaos-Part-of

Detect and Log copy/paste/delete/cut operations in File Explorer

I want to detect and log to file Explorer operations such as copy/cut/delete/paste.
I have read about FileSystemWatcher, but I also noticed it has some issues since there is no copy/cut events available which can be confusing with whatever I want to do.
The operating system and FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.
FileSystemWatcher is hooking the create file and delete file events to the changed and renamed events which can't really help me to determine if it was made by the user or some another process. Furthermore I cannot be 100% sure what happened to the file whether it was copied or cut etc.
I also need to keep track of the locations "from/to" and the name of file.
Is there some alternative to the FileSystemWatcher that can distinguish between these actions?
I think the FileSystemWatcher would be of use in this scenario. You could use the Changed event, which occurs when a file or directory is changed - like a copy/paste action.
See the MSDN Documentation for this event, and the class itself. Note that there are other events that you can also use for the delete/cut actions.
The events use the FileSystemEventArgs which contains properties for FullPath and Name.

C#: Monitoring copied or moved files with FileSystemWatcher

I have come across several "solutions" here and across the web but none seem to fit the bill.
What I am looking to do is have an app monitor a folder for new files (either by creation, a move, or a copy) and perform actions on those objects. That being the scenario, I turned to the FileSystemWatcher class to perform this action.
The problem is that the file FileSystemWatcher.Created event is fired before the entire file is created (most noticeably seen through a copy of a large file).
Is there any way to have this event fire at the conclusion of the file creation as opposed to the beginning? I have tried various combination's of the FileSystemWatcher.NofityFilter property with no success.
Thanks in advance! :)
I have used a couple of solutions for this situation.
If you can work with the creator of the file and use a renaming scheme for the file. EG. Create the File as __Name_ while being created and at the end of the process rename it to Name and the event will fire and you have a complete file.
When your trigger fires check if you can get an exclusive readonly lock on the file. If you can then the write operation has been completed to the file. (I wrote something about this in another question Keep settings in sync between forms application and windows service (or any n-tier, really))
You could possibly integrate something like #2 into your Changed Event and then you'll get the result.
Hmm interesting problem. I never used the object while watching for big files. Did a little searching and seems one solution is to monitor the Changed event as well. Because once the file is done copying (after created is fired) a changed event is thrown as well (cause the file increased in size)
More details from what I read here: http://social.msdn.microsoft.com/forums/en-US/vblanguage/thread/f84bb7c8-b7d5-44da-b0f3-6d1a70415d11/
I know, that what I am going to tell you does not look elegant. I had also to monitor files that arrive from different places, some of them were large and some small. We found out, that FileSystemWatcher is not reliable for this purpose. If you want to be 100% sure, you can check once in a while, using Timer class and its Elapsed event.
You would need to track closing of the file after it's creation and I doubt it's possible with FileSystemWatcher.
If you don't find a solution with FileSystemWatcher, take a look at our CallbackFilter product, which lets you track all operations in real-time.

How can i get user who deleted file?

I need to know which user deleted file in filesystem from c# code.
Only one idea is to use audit, but it seem to be very slow...
You can use the FileSystemWatcher.Deleted event to capture deletes happening on the filesystem.
Depending on the application, you may at that point also be able to find out what user caused this event to occur (it is not part of FileSystemEventArgs).
I don't know if this can be retrieved from the filsystem, but one possible way is to use av FileSystemWatcher object to trigger an event on Deleted. The downside is that you need to have the watcher application running all the time. One upside is that you can monitor just a spesific folder if that's feasible.

Categories