How to open windows explorer when I click a button?
Asked Answered
H

6

10

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?

Hadlee answered 11/8, 2009 at 16:25 Comment(0)
B
29

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

Bergerac answered 11/8, 2009 at 16:46 Comment(2)
Shouldn't PWideChar be used instead of PChar?Kaslik
"explore" works as a verb for folders, and nil does the default (which is Windows explorer for folders). The traditional name for this function is OpenFileDefaultViewer(), which you can google (meant more for "open" than "explore"). It should work on any file type too, based on what's mapped in the registry for the file extension (eg. readme.txt --> notepad). Be careful of the Application assumption; if there's no window (e.g. command-line, or DLL) then you can pass 0 as an invalid handle. The handle is more about Windows managing the active window and window ownership I think.Embroideress
S
13

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
    );
Sigvard answered 11/8, 2009 at 16:44 Comment(0)
A
8

Try this:

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

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

Aguila answered 11/8, 2009 at 16:35 Comment(0)
D
3

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);
Damselfish answered 14/8, 2009 at 14:49 Comment(0)
F
2

As I already answered here there is a difference if you want to

  • select the folder in explorer
  • or open it and show its content with explorer

I use these functions:

uses Winapi.ShellAPI;

procedure SelectFileOrFolderInExplorer(const sFilename: string);
begin
  ShellExecute(Application.Handle, 'open', 'explorer.exe',
    PChar(Format('/select,"%s"', [sFilename])), nil, SW_NORMAL);
end;

procedure OpenFolderInExplorer(const sFoldername: string);
begin
  ShellExecute(Application.Handle, nil, PChar(sFoldername), nil, nil, sw_Show);
end;

procedure ExecuteFile(const sFilename: string);
begin
  ShellExecute(Application.Handle, nil, PChar(sFilename), nil, nil, sw_Show);
end;

Usage:

SelectFileOrFolderInExplorer('C:\Windows');
SelectFileOrFolderInExplorer('C:\Windows\notepad.exe');
OpenFolderInExplorer('C:\Windows');
ExecuteFile('C:\MyTextFile.txt');
Facility answered 17/3, 2022 at 9:3 Comment(0)
G
1

In firemonkey, to open the explorer selecting a file:

uses
  Winapi.Windows,
  Winapi.ShellAPI,
  FMX.Forms,
  FMX.Platform.Win;

procedure OpenExplorerSelectingFile(const AFileName: string);
begin
  ShellExecute(WindowHandleToPlatform(Application.MainForm.Handle).Wnd, 'open', 'explorer.exe', PChar('/select,"' + AFilename + '"'), nil, SW_NORMAL);
end;
Glossal answered 27/10, 2021 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.