在 Windows 资源管理器中检测文件选择

ohs*_*rry 5 c# windows hook windows-explorer

在 Windows 中,无论是在桌面上还是在 Windows 资源管理器中,我想检测文件或文件夹被选中(突出显示)的时刻。发生这种情况时,我想显示一个消息框,显示文件或文件夹的全名。

如果选择了多个项目,我想显示所有项目。

请注意,我的解决方案必须用 C# 编写。

Ren*_*ier 5

看一下这个例子来获取鼠标点击或选择的事件:

/sf/ask/505592461/

加入以下代码,记住添加对 SHDocVW.dll 和 Shell32.dll 的引用,这将返回每个资源管理器中所有选定的项目和文件夹路径。

public void GetListOfSelectedFilesAndFolderOfWindowsExplorer()
    {
        string filename;
        ArrayList selected = new ArrayList();
        var shell = new Shell32.Shell();
        //For each explorer
        foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
        {
            filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower();
            if (filename.ToLowerInvariant() == "explorer")
            {
                Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
                foreach (Shell32.FolderItem item in items)
                {
                    MessageBox.Show(item.Path.ToString());
                    selected.Add(item.Path);
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)