我有一个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添加到您的uses列表中
在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);
您需要在uses子句中添加ShellAPI
。
ShellExecute(Application.Handle, 'explore', '.', nil, nil, SW_NORMAL);
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;
正如我在这里已经回答过的那样,如果您想要:
我使用以下函数:
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;
使用方法:
SelectFileOrFolderInExplorer('C:\Windows');
SelectFileOrFolderInExplorer('C:\Windows\notepad.exe');
OpenFolderInExplorer('C:\Windows');
ExecuteFile('C:\MyTextFile.txt');
PWideChar
而不是PChar
吗? - Leonardo Herrera