Opening a folder in explorer and selecting a file
I’m trying to open a folder in explorer with a file selected.
The following code produces a file not found exception:
How can I get this command to execute in C#?
11 Answers 11
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
If your path contains comma’s, putting quotes around the path will work when using Process.Start(ProcessStartInfo).
It will NOT work when using Process.Start(string, string) however. It seems like Process.Start(string, string) actually removes the quotes inside of your args.
Here is a simple example that works for me.
Just my 2 cents worth, if your filename contains spaces, ie «c:\My File Contains Spaces.txt», you’ll need to surround the filename with quotes otherwise explorer will assume that the othe words are different arguments.
Using Process.Start on explorer.exe with the /select argument oddly only works for paths less than 120 characters long.
I had to use a native windows method to get it to work in all cases:
Samuel Yang answer tripped me up, here is my 3 cents worth.
Adrian Hum is right, make sure you put quotes around your filename. Not because it can’t handle spaces as zourtney pointed out, but because it will recognize the commas (and possibly other characters) in filenames as separate arguments. So it should look as Adrian Hum suggested.
Notice there should be a comma after /select instead of space..
The most possible reason for it not to find the file is the path having spaces in. For example, it won’t find «explorer /select,c:\space space\space.txt».
Just add double quotes before and after the path, like;
or do the same in C# with
You need to put the arguments to pass («/select etc») in the second parameter of the Start method.
It might be a bit of a overkill but I like convinience functions so take this one:
This is the extension function I use as .Quote():
Not the answer you’re looking for? Browse other questions tagged c# explorer or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
C#: How to open Windows Explorer windows with a number of files selected
In the Library of Windows Media Player you can select one or more music files. You can then right-click and in their context menu choose Open File Location. This will open up one windows explorer window for each directory that the files are in, and the files will be selected for you.
So let’s say we have a bunch of mp3 files in our library where three of them are these:
- Z:\Music\Thursday Blues\01. I wish it was friday.mp3
- Z:\Music\Counting Sheep\01. Sheep #1.mp3
- Z:\Music\Counting Sheep\02. Sheep #2.mp3
If we select those three (in a view where all of them are visible) and do Open File Location then two explorer windows will pop up. One will be the Z:\Music\Thursday Blues folder with 01. I wish it was friday.mp3 selected, and the other one will be the *Z:\Music\Counting Sheep** folder with both 01. Sheep #1.mp3 and 02. Sheep #2.mp3 selected.
How can I do this myself in C#? We have an application which is going to export data to various formats, for example CSV and Excel, and I would like to open up explorer windows with these files selected when they are created and ready to be viewed. Currently I just do Process.Start(path) , and this works but I would love to be able to highlight those particular files as well. Would make the files that were just created much more obvious.
Windows Media Player does it so well. I want to do it too =/ Are there any Microsoft employees here that could figure out how it can be done? (A)