Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a folder in Windows Explorer and select a file works second time only

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.

like image 977
Uwe Keim Avatar asked Mar 22 '14 14:03

Uwe Keim


1 Answers

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

This answer contains the required p/invoke/implementation.

like image 85
Alex K. Avatar answered Nov 05 '22 05:11

Alex K.