0

In my C# application (using Visual Studio 2010 ultimate) the user needs to select a certain file. I've created a basic file explorer which pretty much works but doesn't look great.

I've been asked to use the standard Windows file Explorer.

I know how to open it:

Process.Start("explorer.exe");

But how can I get a file path returned from it?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user1711383
  • 778
  • 2
  • 11
  • 20
  • 10
    You should use the [`OpenFileDialog` class](http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx) instead (assuming you are using WinForms as your GUI library). – Uwe Keim Oct 10 '12 at 14:55
  • 1
    explorer.exe is NOT an open/select file dialog. It lets you WORK with your computer. Use OpenFileDialog instead! – Adriano Repetti Oct 10 '12 at 14:56

5 Answers5

4

To select a file, the Net Framework provides the OpenFileDialog component. You can see the reference at MSDN here

But basically, all you have to do is:

Create an Instance of OpenFileDialog

using(OpenFileDialog openFileDialog1 = new OpenFileDialog())
{

Set the initial property

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

Open the control calling the ShowDialog, wait for the OK press from the user and grab the file selected

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string fileSelected = openFileDialog1.FileName;
    }
}

Notice the using statement around the OpenFileDialog(), while not strictly necessary this will assure the Disposing of the dialog

Steve
  • 213,761
  • 22
  • 232
  • 286
  • 2
    I would prefer to put this into a `using` block like `using ( var ofd = new OpenFileDialog() ) { ... }`. – Uwe Keim Oct 10 '12 at 15:01
2

Have you considered an OpenFileDialog?

http://msdn.microsoft.com/en-us/library/aa984392(v=vs.71).aspx

MikeB
  • 2,402
  • 1
  • 15
  • 24
2

You should use the OpenFileDialog class.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
0

Maybe you can convince your customer to accept a standard explorer dialogue. Most likely your customer does not exaclty know what he is asking for.

in WPF you would use something accordingt to this:

OpenDialog for WPF

in Froms you would use something accordingt to this:

http://www.dotnetperls.com/openfiledialog

Community
  • 1
  • 1
Johannes
  • 6,490
  • 10
  • 59
  • 108
0

It's a little complicated because "explorer.exe" is in %PATH% system variable, and executable path is probably not stored anywhere in Windows (in my opinion, maybe im wrong).

"explorer.exe" in all versions of windows i know is located in directory that is defined by %WINDIR% system variable. You can get that variable by method:

Environment.GetEnvironmentVariable()

and add string "explorer.exe".

That was simple way.


More complicated method:

You can find full path of already running explorer.exe process after by using WMI - little more complicated, but its more correct method.

More information here: How to get full path of running process

Community
  • 1
  • 1
Kamil
  • 13,363
  • 24
  • 88
  • 183