10

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.

Lodle
  • 31,277
  • 19
  • 64
  • 91

3 Answers3

27

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)
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
2

This does not show the command window, just opens the directory.


system("explorer C:\\");

berlindev
  • 1,753
  • 14
  • 22
  • @berlindev Where does 'system' come from? – TinyRacoon Oct 18 '19 at 09:03
  • @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
0

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++

yu yang Jian
  • 6,680
  • 7
  • 55
  • 80