22

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.

devios1
  • 36,899
  • 45
  • 162
  • 260
  • Possible duplicate of [Open Folder and Select the file](http://stackoverflow.com/questions/334630/open-folder-and-select-the-file) – RandomEngy Sep 11 '16 at 17:00

3 Answers3

54

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
  • 5,502
  • 3
  • 33
  • 59
  • 1
    Thanks! To my surprise, when Directory Opus is installed, this is used instead, despite the explicit reference to explorer.exe. This is a good thing. – Crosbie Sep 24 '11 at 09:55
  • @Crosbie that's because Directory Opus hooks into explorer and intercepts calls to it. – Mahmoud Al-Qudsi Sep 26 '16 at 12:36
5

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

System.Diagnostics.Process.Start(
    "explorer.exe", 
    string.Format("/select, \"{0}\"", filename));
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • This won't work because `select`'s parameter should be comma-separated rather than space-separated – Regent May 13 '10 at 19:12
  • 3
    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 May 13 '10 at 21:22
1

Containing folder, Self directory is represented in many ways!!! Simple 2 ways are . and, .\. no idea what is the difference!.. :D From DOS and bat files... Start . or Start .\. (Y)

Try... these 2 works, but check whether this is the solution u expect!

System.Diagnostics.Process.Start("explorer.exe", @".\.");

Or

System.Diagnostics.Process.Start("explorer.exe", @".");

-

  • Sometimes the application is run from a temp directory or a different dir (eg: in Sandbox... or while being scanned by antivirus... etc. :)
Akila DJ
  • 19
  • 2