views:

142

answers:

2
+1  Q: 

Open in Explorer

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.

A: 

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


system("explorer C:\\");

joki
+6  A: 

You probably are looking for the ShellExecute() function in shell32.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:

ShellExecute(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:

ShellExecute(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)
Works a treat and i didnt need to init the com interface thing as well
Lodle