简体   繁体   中英

Monitor file selection in explorer (like clipboard monitoring) in C#

I am trying to create a little helper application, one scenario is "file duplication finder". What I want to do is this:

  • I start my C# .NET app, it gives me an empty list.
  • Start the normal windows explorer, select a file in some folder
  • The C# app tells me stuff about this file (eg duplicates)

How can I monitor the currently selected file in the "normal" windows explorer instance. Do I have to start the instance using .NET to have a handle of the process. Do I need a handle, or is there some "global hook" I can monitor inside C#. Its a little bit like monitoring the clipboard, but not exactly the same...

Any help is appreciated (if you don't have code, just point me to the right interops, dlls or help pages :-) Thanks, Chris

EDIT 1 (current source, thanks to Mattias)

using SHDocVw;
using Shell32;

public static void ListExplorerWindows()
{
    foreach (InternetExplorer ie in new ShellWindowsClass())
        DebugExplorerInstance(ie);
}

public static void DebugExplorerInstance(InternetExplorer instance)
{
    Debug.WriteLine("DebugExplorerInstance ".PadRight(30, '='));
    Debug.WriteLine("FullName " + instance.FullName);
    Debug.WriteLine("AdressBar " + instance.AddressBar);
    var doc = instance.Document as IShellFolderViewDual ;
    if (doc != null)
    {
        Debug.WriteLine(doc.Folder.Title);
        foreach (FolderItem item in doc.SelectedItems())
        {
            Debug.WriteLine(item.Path);
        }
    }
}

You can do this with the shell automation interfaces. The basic process is to

  1. Run Tlbimp on Shdocwv.dll and Shell32.dll (or directly add a reference from VS).
  2. Create an instance of the ShellWindows collection and iterate. This will contain both Windows Explorer and Internet Explorer windows.
  3. For Windows Explorer windows, the IWebBrowser2.Document property will return a IShellFolderViewDual reference.
  4. The IShellFolderViewDual has a SelectedItems method you can query and an event for changes you can handle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM