Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file location

Tags:

c#

file

winforms

When searching a file in Windows Explorer and right-click a file from the search results; there is an option: "Open file location". I want to implement the same in my C# WinForm. I did this:

if (File.Exists(filePath)
{
    openFileDialog1.InitialDirectory = new FileInfo(filePath).DirectoryName;
    openFileDialog1.ShowDialog();
}

Is there any better way to do it?

like image 510
Haroon A. Avatar asked Mar 10 '12 11:03

Haroon A.


People also ask

Where is Open file location?

To open File Explorer, click on the File Explorer icon located in the taskbar. Alternatively, you can open File Explorer by clicking on the Start button and then clicking on File Explorer.

Is there a shortcut to open file location?

Press Alt+F to open the File menu.

How do I open an apps location?

On your phone's home screen, find the app icon. Touch and hold the app icon. Tap App info . Location.


1 Answers

If openFileDialog_View is an OpenFileDialog then you'll just get a dialog prompting a user to open a file. I assume you want to actually open the location in explorer.

You would do this:

if (File.Exists(filePath))
{
    Process.Start("explorer.exe", filePath);
}

To select a file explorer.exe takes a /select argument like this:

explorer.exe /select, <filelist>

I got this from an SO post: Opening a folder in explorer and selecting a file

So your code would be:

if (File.Exists(filePath))
{
    Process.Start("explorer.exe", "/select, " + filePath);
}
like image 137
gideon Avatar answered Sep 23 '22 16:09

gideon