I am using a non win forms program where I need to open the file explorer and return the file that the user selects. I am using Process.Start("explorer.exe", "/select etc");
to open file explorer, but when I select a file it just opens that file. Is there a way that when I select the file it will return the file path to a variable?
Asked
Active
Viewed 37 times
0

Aaron
- 4,380
- 19
- 85
- 141
-
1Possible duplicate of [How do I add a form to a console app so that user can select file?](http://stackoverflow.com/questions/12553932/how-do-i-add-a-form-to-a-console-app-so-that-user-can-select-file) – NineBerry Mar 22 '16 at 17:47
1 Answers
3
I'm sure your approach is wrong. As the documentation[1] states:
/select, Opens a window view with the specified folder, file or application selected.
So the /select
command line argument only tells the explorer to pre-select the specified folder or file. It is not for returning any user selection.
I suggest to use OpenFileDialog
. You can use this dialog even if your application is not a pure Windows Forms app. You just need to reference the needed assemblies (System.Windows.Forms.dll
) and namespaces (using System.Windows.Forms;
).
[1] Link outdated: archive

René Vogt
- 43,056
- 14
- 77
- 99
-
1The `OpenFileDialog` class also rests in the `Microsoft.Win32` namespace, so the reference to `System.Windows.Forms` is unnecessary. – Fᴀʀʜᴀɴ Aɴᴀᴍ Mar 22 '16 at 17:52
-
Since this is a console app, it's better not to use the WinForms namespace, I guess. – Fᴀʀʜᴀɴ Aɴᴀᴍ Mar 22 '16 at 17:53
-
Thanks for the advice. I only read msdn where they say `OpenFileDialog` is declared in `System.Windows.Forms`. If it works without that reference...nice. – René Vogt Mar 22 '16 at 17:55
-
Here -> https://msdn.microsoft.com/en-us/library/microsoft.win32.openfiledialog(v=vs.110).aspx. +1 for being able to understand what the OP wants :) – Fᴀʀʜᴀɴ Aɴᴀᴍ Mar 22 '16 at 17:58