Open File Location

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

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

Is there a better way to do this?

+7
source share
2 answers

If openFileDialog_View is OpenFileDialog , then you just get a dialog box asking you to open the file. I assume that you really want to open the location in explorer.

You would do this:

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

The select file explorer.exe accepts the /select argument as follows:

 explorer.exe /select, <filelist> 

I got this from SO message: Opening a folder in Explorer and selecting a file

So your code will look like this:

 if (File.Exists(filePath)) { Process.Start("explorer.exe", "/select, " + filePath); } 
+28
source

This is how I do it in my code. This will open the file directory in Explorer and select the specified file as Windows Explorer does.

 if (File.Exists(path)) { Process.Start(new ProcessStartInfo("explorer.exe", " /select, " + path); } 
+4
source

All Articles