C# 将我的程序与 Windows 资源管理器的重命名事件挂钩

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:13:43  来源:igfitidea点击:

Hooking my program with windows explorer's rename event

提问by pek

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

例如:用户重命名文件并按 Enter(或单击离开)以确认重命名操作。在文件实际重命名之前,我的程序“侦听”此事件并弹出一条消息,提示“您确定要将 C:\test\file.txt 重命名为 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.

我在想/希望 C++、C# 或 .NET 可以实现这一点。但我不知道在哪里寻找。

采纳答案by Riri

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

您可以通过使用.NET 框架中的FileSystemWatcher 类来解决这个问题。

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.

您可以监视文件或目录的重命名、删除或创建。例如,要监视文本文件的重命名,请将 Filter 属性设置为“*.txt”并使用为其参数指定的 Renamed 调用 WaitForChanged 方法。

回答by thelsdj

My guess is that this is notpossible, I did find thiswhich 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.

@Richard,如果您只需要监视更改,则 FileSystemWatcher 很好,但他需要中断它们,而它无法做到。

回答by Mark Brackett

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

IFileOperationProgressSink.PreRenameItem是我所知道的最接近支持的东西。不幸的是,它不是资源管理器的挂钩 - 因此您只能将它用于您自己的IFileOperation操作。根据您的需要,您可以编写一个 shell 扩展来完成您自己的ConfirmRename(或其他)操作,并从那里分支。

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.

否则,你正在寻找 hooking SHFileOperation,我想。这必须在非托管代码中完成,因为您将被加载到 Explorer.exe。对于 Vista,这已更改为IFileOperation- 这可能意味着您必须挂钩它的创建并传递您的模拟。

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

就我个人而言,我认为既然您在谈论重命名,那么 wilhelmtell在更改确认并在必要时撤消它的想法是最好的主意。