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

Using the code described in this answer of SO's publication β€œOpen a folder and select a file” , I 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 problem:

Calling the function for the first time for a specific file, Windows Explorer displays correctly with the file folder, but 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 selects the file.

eg. the first call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") opens the folder "C: \ MyFolder" in a new window in Windows Explorer. The second call to OpenExplorerAndSelectFile("C:\MyFolder\MyFile.txt") activates this window again and selects MyFile.txt .

Doing something like this, for example. Google Chrome (going to the download page and displaying a previously downloaded file) actually works well on the first try.

So, I came to the conclusion that Google Chrome seems to do this a little differently than me.

My question is:

Is there any way to debug / track the Win32 / Shell method that calls Google Chrome?

Then I compared them to what I do to see the differences.

+6
source share
2 answers

Instead of the command line explorer, Chrome most likely uses the more flexible SHOpenFolderAndSelectItems Shell API.

This answer contains the required implementation of p / invoke /.

+1
source

Try using the SHOpenFolderAndSelectItems wrapper function .

-3
source

All Articles