Gener4tor
Gener4tor

Reputation: 388

Trigger when the user is deleting a file

In order to play a sound my c# program has to trigger every time the user deletes a file via the explorer in windows.

How can I do this? Is this even possible?

Edits:

To clarify: I want this to be some kind of a system hook. So it should not matter where the file is deleted.

I tried this:

m_watcher = new FileSystemWatcher();
m_watcher.Path = @"C:\";
m_watcher.Filter = "*.*";
m_watcher.IncludeSubdirectories = true;
m_watcher.EnableRaisingEvents = true;

m_watcher.Deleted += M_watcher_Deleted;

private void M_watcher_Deleted(object sender, FileSystemEventArgs e)
{
    Debug.WriteLine("deleted");
}

but the problem is that I cant find out if M_watcher_Deleted is called as a result of a direct user action like pressing delete in the explorer. This is important because I want to play a sound. And if I am not able to differentiate between user actions and background actions with temporary files the sound will play all the time.

Upvotes: 0

Views: 435

Answers (2)

Luaan
Luaan

Reputation: 63722

There is no separate API for "file system actions initiated by the user" and "file system actions initiated by some automatic system". It shouldn't be too surprising, since there's no objective way to judge the difference in the first place.

If I use explorer to move a file, is that a user action or a system action (I said "move file", not "copy here, delete there")? If I have a piece of text editing software and save a file, is that user action or system action? If the editor also saves a backup file, is that a user action, or a system action? If the OS also keeps its own file history?

I think we can agree that there is a sliding scale here - the first case is the most obviously user-related, and the last most obviously "behind the scenes". But they're all initiated by the user. And that's just with a single file being "directly" acted upon.

The system doesn't keep track of the series of decisions and actions that led to a file system operation (and if it did, good luck interpreting that in any useful fashion). It does know about which process initiated the operation, for example, but that's not something you should take into account outside of debugging. Just because an operation was initiated by explorer.exe doesn't mean that it comes from the user dragging a file from one place to another in Explorer.

You need to think about what kind of service you want to provide to your user. Thinking about why I would every consider a feature like this useful, the first thing that comes to my mind is that you shouldn't care about how exactly a file was deleted, but rather, where the file was - that is, having a list of directories that you keep watch of. If I (or something else!) deletes a file from my documents, that might be worth my attention; if I (or something else) delete a file from the Temp folder, why should I care?

Upvotes: 1

dbraillon
dbraillon

Reputation: 1752

If you want to detect any file deletion and if you want to know if it was by the user or not, then it's currently not possible in any .Net implementation regarding this answer.

FileSystemWatcher does not receive any hint on the origin of that deletion.

Upvotes: 1

Related Questions