Programmatically selecting a file in windows explorer!
Posted by Nibu Thomas on December 19, 2007
Well while coding for process viewer I felt the need for programmatically selecting a file in windows explorer and after a short search bumped into about.com which had a code snippet in Delphi which does this.
Modified a “bit” for VC++…
// Select a file in explorer
void SelectFileInExplorer( LPCTSTR lpctszFileToSelect_i )
{
// This is the command line for explorer which tells it to select the given file
CString csCommandLine = _T( "/Select," );
csCommandLine += lpctszFileToSelect_i;
// Prepare shell execution params
SHELLEXECUTEINFO shExecInfo = { 0 };
shExecInfo.cbSize = sizeof( shExecInfo );
shExecInfo.lpFile = _T( "Explorer.exe" );
shExecInfo.lpParameters = csCommandLine;
shExecInfo.nShow = SW_SHOWNORMAL;
shExecInfo.lpVerb = _T( "Open" ); // Context menu item
// Just have a look in MSDN to see the relevance of these flags
shExecInfo.fMask = SEE_MASK_INVOKEIDLIST | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
// Select file in explorer
VERIFY( ShellExecuteEx( &shExecInfo ));
}// End SelectFileInExplorer








Nibu Thomas said
Hi,
Did you try with a comma, what happened?
bibibubu said
But what if there are comma sign “,” in file name?
bibibubu
Nibu Thomas said
Thanks mate! Useful for people trying to do this in C#.
Artur Perwenis said
And in C# the shorter version is:
private void Locate_Click(string fileName)
{
string cmdLine = string.Format(CultureInfo.InvariantCulture, “/Select,{0}”, fileName);
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = “explorer.exe”;
psi.Arguments = cmdLine;
var proc = Process.Start(psi);
}
Nibu Thomas said
Ah ok, Sorry I misunderstood.
jongampark said
No.. what I mean is..
For example, when you open a file in a Visual Studio, you click its file name from its title or in the Solution Explorer, and do “Open its containing folder” or “Locate it in the Explorer” and so on.
Currently, in tabbed window scheme of the VS, it provides “Open its containing folder”, but in other scheme, it doesn’t provide such a feature. Also, the Solution Explorer doesn’t have the feature.
In the “Multiple Windows” layout, it is pretty hard to confirm the opened file’s location. Using property pane is sometimes impossible.
Nibu Thomas said
I guess its already there in the Find files application. Isn’t it?
jongampark said
nibuthomas said
Bartosz Wójcik said
It must be a coincidence but that was what i was looking for right now
)) and i was browsing CodeProject and saw your blog address
)
Thnx man