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?
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?
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
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
);
Try this:
ShellExecute(Application.Handle, nil, 'explorer.exe', nil, nil, SW_NORMAL);
You'll need to add ShellAPI
to your uses clause.
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);
As I already answered here there is a difference if you want to
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');
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;
© 2022 - 2024 — McMap. All rights reserved.
PWideChar
be used instead ofPChar
? – Kaslik