views:

156

answers:

2

This can be a handy functionality to have in a program that works with files/folders. It's easy enough to actually open the containing folder using:

System.Diagnostics.Process.Start( *path to folder* );

...but how do I go about actually selecting the target file within that parent folder? If I use the Process.Start method it actually attempts to open the file.

+6  A: 

According to Windows Explorer Command-Line Options you just need to start an explorer process with /select parameter.

For instance, 'explorer /select,c:\Windows' will open a window with c:\windows folder selected.

So simply Process.Start("explorer.exe", "/select," + filename) should be enough.

Regent
+2  A: 

Execute Explorer.exe with /select, "filename" command line argument

System.Diagnostics.Process.Start(
    "explorer.exe", 
    string.Format("/select, \"{0}\"", filename));
Chris Taylor
This won't work because `select`'s parameter should be comma-separated rather than space-separated
Regent
@regent, thanks fixed the typo.
Chris Taylor
Works great! Thanks!
chaiguy
Just for a note, quoting (`\"{0}\"`) is not necessary because `explorer` will treat anything after `/select,` as a path (ignoring starting and ending whitespace) -- so '`/select,{0}`' is enough...
Regent
Cool thanks....
chaiguy