Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting file-selection in Windows Explorer [closed]

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#.

like image 347
ohsorry Avatar asked Nov 18 '12 00:11

ohsorry


Video Answer


2 Answers

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);
                }
            }
        }
    }
like image 129
Renier Avatar answered Sep 22 '22 15:09

Renier


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
like image 23
Nguyen Minh Hien Avatar answered Sep 24 '22 15:09

Nguyen Minh Hien