views:

806

answers:

3

In Windows Media Player you can right click on a music file and click 'open file location' and it opens an explorer window with the file selected.

I want to be able to do this in applications. So given a filename (as an ansi string) I want to be able to go to the file in a new explorer window.

A: 

You should use the ShellExecute() function. Something like this should work:

ShellExecute(handle, "explore", @"C:\WINDOWS", NULL, NULL, SW_SHOWNORMAL);
Scott W
I think that only works for folders, not files. I want to explore _and_ select the file.
MrVimes
I figured out the command line for this... explorer /select,(filename) so I now need to figure out how to run that shell command
MrVimes
+1  A: 

Try launching explorer.exe with option /select,<selected file path>. Something like:

explorer /n, /select,c:\windows\notepad.exe

It should open new explorer window with c:\windows folder open and notepad.exe selected.

Tomek Szpakowicz
can I run this using shellexecute()? I can run it on the command line and it works, but when I run in shellexecute nothing happens. ShellExecute(Form1->Handle,NULL,String("explorer.exe /n, /select," + FileName).c_str(),NULL,NULL,SW_SHOWNORMAL);
MrVimes
OK I'm tearing my hair out trying to run that in an application so I guess I'll ask a fresh question on stackoverflow.
MrVimes
What about: ShellExecute(Form1->Handle,"open","c:\\windows\\explorer.exe", String("/n, /select," + FileName).c_str(),NULL,SW_SHOWNORMAL);Or use one of _spawn functions. See documentation for more.
Tomek Szpakowicz
A: 

It's a Win2 FAQ for more than 16 years (!)

Search on Google Groups before posting a so basic question where the C code has been posted 25000 times...

Very helpful. Thanks.
MrVimes