tags:

views:

761

answers:

5

Is there any way, in any language, to hook my program when a user renames a file?

For example: A user renames a file and presses enter (or clicks away) to confirm the rename action. BEFORE the file is actually renamed, my program "listens" to this event and pops up a message saying "Are you sure you want to rename C:\test\file.txt to C:\test\test.txt?".

I'm thinking/hoping this is possible with C++, C# or .NET.. But I don't have any clue where to look for.

Thank you.

A: 

My guess is that this is not possible, I did find this which is for monitoring operations (including rename) on a folder, but there does not appear to be a similar method for files.

@Richard, FileSystemWatcher is good if you only need to monitor changes, but he needs to interrupt them which it cannot do.

thelsdj
+4  A: 

You can probably solve this by using the FileSystemWatcher class in .NET framework.

From the class remarks:

You can watch for renaming, deletion, or creation of files or directories. For example, to watch for renaming of text files, set the Filter property to "*.txt" and call the WaitForChanged method with a Renamed specified for its parameter.

Riri
A: 

What if the program sits in the system tray. Would this make any difference..?

pek
+2  A: 

@thelsdj

How about noticing the change and undoing it?

wilhelmtell
A: 

(VS.85).aspx">IFileOperationProgressSink.PreRenameItem is the closest supported thing I know of. Unfortunately, it's not a hook into Explorer - so you can only use it for your own IFileOperation actions. Depending on your needs, you can write a shell extension to do your own ConfirmRename (or something), and branch from there.

Otherwise, you're looking at hooking SHFileOperation, I think. This would have to be done in unmanaged code, as you'll be loaded into Explorer.exe. For Vista, this has been changed to IFileOperation - which probably means you'll have to hook the creation of it and pass out your mock.

Personally, I think since you're talking a rename, wilhelmtell's idea of confirming after the change, and undoing it if necessary is the best idea.

Mark Brackett