How do you open a path in explorer by code in c++. I googled and couldn't find any thing but systems commands to do this, however, i dont want it to block or show the console window.
3 Answers
You probably are looking for the ShellExecute()
function in shellapi.h
. It is called with an "action verb", a path, and optional parameters. In your case this will want either "open" or "explore" as follows:
ShellExecuteA(NULL, "open", "C:\\", NULL, NULL, SW_SHOWDEFAULT);
This will open an unattached explorer window at C:. ShellExecute() will give basically the same action as typing in a command at the Run dialog. It will also handle URLs, so the following will open up the user's default browser:
ShellExecuteA(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWDEFAULT);
Although make sure to pay attention to the note in the documentation that ShellExecute relies on COM (Although your code doesn't have to worry about any COM objects itself).
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)

- 17,329
- 10
- 113
- 185
-
Works a treat and i didnt need to init the com interface thing as well – Lodle Dec 10 '08 at 05:15
-
error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR' – john k Jan 06 '13 at 21:52
-
The header to include is Shellapi.h. – M Katz Sep 19 '16 at 05:42
-
Didn't know about "action verbs" at all. Thanks. (There's a back slash missing in the open example). – TinyRacoon Oct 18 '19 at 09:04
This does not show the command window, just opens the directory.
system("explorer C:\\");

- 1,753
- 14
- 22
-
-
@Harsh kurra You probably need to put your path in quotes. e.g. system("explorer \"C:\\me path with spaces\\". Otherwise explorer will think your path is multiple parameters. – TinyRacoon Oct 18 '19 at 09:06
I'm now using VS2017, using as follows works:
ShellExecute(NULL, L"open", L"YourFolderPath\\YourFile.xxx", NULL, NULL, SW_RESTORE);
also reference ShellExecute to open an .exe in C++

- 6,680
- 7
- 55
- 80