简体   繁体   中英

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.

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)

这不会显示命令窗口,只是打开目录。


system("explorer C:\\");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM