tags:

views:

273

answers:

4

I have a form in a Delphi project. There is a button on the form. When the user clicks the button, I want it to open Windows Explorer.

What code will I need to achieve this?

+7  A: 

Try this:

ShellExecute(Application.Handle, nil, 'explorer.exe', nil, nil, SW_NORMAL);

You'll need to add ShellAPI to your uses clause.

Mason Wheeler
+3  A: 

Building on what Mason Wheeler said: you can also pass in a directory as an argument, to get the window to open to a non-default location:

uses
  ShellAPI;

...

  ShellExecute(Application.Handle,
    nil,
    'explorer.exe',
    PChar('c:\'), //wherever you want the window to open to
    nil,
    SW_NORMAL     //see other possibilities by ctrl+clicking on SW_NORMAL
    );
JosephStyons
+12  A: 

Well in case you need to select some particular file in explorer I have the following function which I use

procedure SelectFileInExplorer(const Fn: string);
begin
  ShellExecute(Application.Handle, 'open', 'explorer.exe',
    PChar('/select,"' + Fn+'"'), nil, SW_NORMAL);
end;

and you can call it :

SelectFileInExplorer('C:\Windows\notepad.exe');

EDIT: As mentioned ShellAPI must be added to your uses list

Aldo
very cool, i haven't seen that before
JosephStyons
A: 

According to http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx, ShellExecute also supports the 'explore' verb, which 'explores' a folder specified by lpFile, so this should work:

ShellExecute(Application.Handle, 'explore', '.', nil, nil, SW_NORMAL);
mjustin