5

In Windows, either on the desktop or in Windows Explorer, I want to detect the moment when a file or folder is selected (highlighted). When that happens, I want to display a message box showing the file or folder's full name.

If there are multiple items selected, I want to display all of them.

Note that my solution must be written in C#.

Troy Alford
  • 26,660
  • 10
  • 64
  • 82
ohsorry
  • 83
  • 1
  • 4
  • Have you read [this](http://stackoverflow.com/questions/4921413/get-selected-file-or-folder-in-windows-explorer) or [this](http://stackoverflow.com/questions/3382946/get-selected-items-of-folder-with-winapi/3400348#3400348)? – khellang Nov 18 '12 at 01:45
  • thanyou,khellang,yes,i'v read them.but i still confused.i need a clear C# code. – ohsorry Nov 18 '12 at 02:45
  • im trying this:IShellFolderViewDual2 – ohsorry Nov 18 '12 at 02:52
  • dose anyone can help me?In Window 7,there is a detail bar in the bottom of Explorer window,when user select a file,the bar will show some more detail information about the file. that is exactly what i want to do. – ohsorry Nov 18 '12 at 04:53
  • 1
    The correct way to do this is not to try to inject additional content into Explorer but rather to host Explorer yourself (ExplorerBrowser object) and listen for selection events. – Raymond Chen Mar 15 '13 at 21:46
  • This might be a related question: http://stackoverflow.com/questions/8292953/get-current-selection-in-windowsexplorer-from-a-c-sharp-application – maximpa Mar 26 '13 at 03:10

2 Answers2

5

Take a look at this example to get the mouse click or selected events:

https://stackoverflow.com/questions/7222749/i-created-a-program-to-hide-desktop-icons-on-double-click-of-desktop-but-would-o

Join that with the following code, Remember to add reference to SHDocVW.dll and Shell32.dll this will return all the selected items and folders paths in every explorer.

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);
                }
            }
        }
    }
peterflynn
  • 4,667
  • 2
  • 27
  • 40
Renier
  • 1,738
  • 3
  • 24
  • 51
2

Just adding somethings to Renier's answer:

  • SHDocVW.dll and Shell32.dll are in the folder C:\Windows\System32
  • If you get error at SHDocVw.ShellWindowsClass() just right-click on the SHDocVw reference on your Solution Explorer then select Properties and set Embed Interop Types to false
Nguyen Minh Hien
  • 455
  • 7
  • 10
  • 1
    If you use `SHDocVw.ShellWindows()` instead of `SHDocVw.ShellWindowsClass()` then you won't have to fiddle with the "Embed Interop Types" setting. See https://stackoverflow.com/a/4174056/1172352. – peterflynn Apr 27 '20 at 01:00