9

Using the code described in this answer of the SO posting "Open folder and select the file", I've created this function:

public static void OpenExplorerAndSelectFile(string filePath)
{
    Process.Start(
        @"explorer.exe",
        string.Format(@"/select, ""{0}""", filePath));
}

This function works well, with one small issue:

Calling the function for the first time for a specific file, Windows Explorer is correctly shown with the folder of the file, but it does not select the file.

Calling the same function again for the same file, it switches back to the already open folder in Windows Explorer and then it selects the file.

E.g. the first call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") opens the folder "C:\MyFolder" in a new Windows Explorer Window. The second call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") actually activates that Window again and selects MyFile.txt.

Doing something similar in e.g. Google Chrome (Going to the download page and showing a previously downloaded file) actually works well right in the first try.

So my conclusion is that Google Chrome seems to do it a bit different than I do.

My question:

Is there a way to debug/trace the Win32/Shell method that Google Chrome calls?

I would then compare them to what I do to see the differences.

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    I can't reproduce this on Win7/x64. For me it selects the file on the first time, but it doesn't re-use the open window, it always creates a new one – rene Mar 22 '14 at 14:26
  • 2
    `SHOpenFolderAndSelectItems` : http://stackoverflow.com/questions/1073353/c-how-to-open-windows-explorer-windows-with-a-number-of-files-selected?rq=1 – Alex K. Mar 22 '14 at 14:31
  • @AlexK. This works perfectly, thanks a lot! Maybe you can put this as an answer so I can accept it? – Uwe Keim Mar 22 '14 at 14:45

2 Answers2

2

Rather than the explorer command line Chrome most probably uses the more flexible SHOpenFolderAndSelectItems Shell API.

This answer contains the required p/invoke/implementation.

Community
  • 1
  • 1
Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Still, this solution doesn't work for me if performed on a newly created / renamed / copied folder. Can someone please verify? – gil_mo Mar 30 '22 at 11:30
-3

Try using the shell function "SHOpenFolderAndSelectItems".