Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening an explorer window with designated file selected

I have an application which has an option to show the selected file in the folder in which the file resides. My question is, how do I achieve this?

To clarify, if a user in my program selected the "Test.txt" file, then I want a Windows Explorer window to pop up and highlight the file the user selected. You can see similar behavior in LimeWire and uTorrent. If you select a file in either of those programs and choose "Show in Folder", it pops up a Windows Explorer window with the file highlighted and selected. I am trying to duplicate this behavior.

I tried using the following line:

System.Diagnostics.Process.Start("Explorer");

This will popup the Windows Explorer window, however, it always seems to open up by default in "My Documents" folder.

like image 561
Icemanind Avatar asked Oct 08 '10 02:10

Icemanind


People also ask

How do I make Windows Explorer open to a specific folder?

Set Default Folder to This PC To change the setting, open Explorer, click on File and then click on Change folder and search options. In the dialog that pops up, you should already be on the General tab. At the very top, you'll see Open File Explorer to where you can select from This PC and Quick Access.

How do I change where File Explorer opens?

By default, File Explorer opens to Quick access. If you'd rather have File Explorer open to This PC, go to the View tab and then select Options. In the Open File Explorer to: list, select This PC, and then select Apply.

When you open Windows Explorer I am the folder that is selected by default?

When you open Windows Explorer in Windows 10, the default folder that opens is either Quick Access or This PC, depending on your settings.

How do I open a specific file path?

You can also freely specify an app to open the file. You need to type the whole path of the app ahead of the path of the file, for example, "%windir%\system32\mspaint.exe" "C:\Users\mini\Desktop\travel. png". Press Enter to open the file using CMD.


1 Answers

Here you go,

string fileToSelect = @"C:\temp.img";
string args = string.Format("/Select, \"{0}\"", fileToSelect);

ProcessStartInfo pfi = new ProcessStartInfo("Explorer.exe", args);
System.Diagnostics.Process.Start(pfi);

Note: Adding \" before and after the {0} parameter enables the fileToSelect string to contain spaces (i.e. "C:\My Documents").

From this Thread:
Programmatically select multiple files in windows explorer

Cheers,

like image 117
Karthik Mahalingam Avatar answered Oct 07 '22 00:10

Karthik Mahalingam