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

倾然丶 夕夏残阳落幕 提交于 2019-12-08 17:13:55

问题


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.


回答1:


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

This answer contains the required p/invoke/implementation.




回答2:


Try using the shell function "SHOpenFolderAndSelectItems".



来源:https://stackoverflow.com/questions/22578757/open-a-folder-in-windows-explorer-and-select-a-file-works-second-time-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!