我在 Delphi 项目中有一个表格。表格上有一个按钮。当用户单击按钮时,我希望它打开 Windows 资源管理器。
我需要什么代码来实现这一点?
好吧,如果您需要在资源管理器中选择某些特定文件,我将使用以下功能
procedure SelectFileInExplorer(const Fn: string);
begin
ShellExecute(Application.Handle, 'open', 'explorer.exe',
PChar('/select,"' + Fn+'"'), nil, SW_NORMAL);
end;
你可以称之为:
SelectFileInExplorer('C:\Windows\notepad.exe');
编辑:如前所述,必须将 ShellAPI 添加到您的使用列表中
基于 Mason Wheeler 所说:您还可以将目录作为参数传递,以使窗口打开到非默认位置:
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
);
尝试这个:
ShellExecute(Application.Handle, nil, 'explorer.exe', nil, nil, SW_NORMAL);
您需要添加ShellAPI
到您的使用条款。
根据http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx,ShellExecute还支持“探索”动词,它“探索”由 lpFile 指定的文件夹,所以这应该工作:
ShellExecute(Application.Handle, 'explore', '.', nil, nil, SW_NORMAL);
在 firemonkey 中,打开资源管理器选择一个文件:
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;