How do I detect if Windows Explorer is displaying

2019-06-10 00:50发布

问题:

My program creates a folder in the executable's directory and saves some output files. When the application finishes, I have it automatically open the folder in an explorer window using ShellExecute(NULL, _T("Open"), _T("Explorer"), _T(m_strOutputPath), NULL, SW_SHOWDEFAULT);

Now, if I keep re-running the program, it will just keep opening the same folder even if it is already opened. For the absent-minded user (with me being one of them), this results in multiple windows of the same folder.

Is there a way to detect if the folder is already opened in an explorer window and not make it open a new window if it is already opened?

I'm developing in MSVS 2008 SP1 on Windows Vista, but the program will run on XP, Vista, and 7.

回答1:

You can use the SHOpenFolderAndSelectItems function. It will open a folder window and select the files given. If the window is already open, it'll bring it to the front.

Internally, it uses the IShellWindows interface, specifically the FindWindowSW method.



回答2:

Try this:

ShellExecute(NULL, _T("Open"), _T(m_strOutputPath), NULL, NULL, SW_SHOWDEFAULT);