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

<p>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?".</p>

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

[{'answer_id': 24664, 'author': 'thelsdj', 'author_id': 163, 'author_profile': 'https://Stackoverflow.com/users/163', 'pm_score': 0, 'selected': False, 'text': '<p>My guess is that this is <em>not</em> possible, I did find <a href="http://msdn.microsoft.com/en-us/library/bb776794(VS.85).aspx" rel="nofollow noreferrer">this</a> which is for monitoring operations (including rename) on a folder, but there does not appear to be a similar method for files.</p>\n\n<p>@Richard, FileSystemWatcher is good if you only need to monitor changes, but he needs to interrupt them which it cannot do.</p>\n'}, {'answer_id': 24671, 'author': 'Riri', 'author_id': 298, 'author_profile': 'https://Stackoverflow.com/users/298', 'pm_score': 4, 'selected': True, 'text': '<p>You can probably solve this by using the <a href="http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx" rel="noreferrer">FileSystemWatcher class</a> in .NET framework.</p>\n\n<p>From the class remarks:</p>\n\n<blockquote>\n  <p>You can watch for renaming, deletion,\n  or creation of files or directories.\n  For example, to watch for renaming of\n  text files, set the Filter property to\n  "*.txt" and call the WaitForChanged\n  method with a Renamed specified for\n  its parameter.</p>\n</blockquote>\n'}, {'answer_id': 24781, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/bb775745(VS.85).aspx" rel="nofollow noreferrer"><code>IFileOperationProgressSink.PreRenameItem</code></a> 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 <code>IFileOperation</code> actions. Depending on your needs, you can write a shell extension to do your own <code>ConfirmRename</code> (or something), and branch from there.</p>\n\n<p>Otherwise, you\'re looking at hooking <code>SHFileOperation</code>, 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 <code>IFileOperation</code> - which probably means you\'ll have to hook the creation of it and pass out your mock.</p>\n\n<p>Personally, I think since you\'re talking a rename, <a href="https://stackoverflow.com/questions/24644/hooking-my-program-with-windows-explorers-rename-event#24742">wilhelmtell\'s idea</a> of confirming <em>after</em> the change, and undoing it if necessary is the best idea.</p>\n'}]
['https://Stackoverflow.com/questions/24644', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2644/']