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?
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.
Press Alt+F to open the File menu.
On your phone's home screen, find the app icon. Touch and hold the app icon. Tap App info . Location.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With