打开Windows资源管理器目录,选择一个特定文件(在Delphi中) [英] Open Windows Explorer directory, select a specific file (in Delphi)
问题描述
我有一个过程可以在Windows资源管理器中打开一个传递目录路径的文件夹:
I have a procedure to open a folder in Windows Explorer that gets passed a directory path:
procedure TfrmAbout.ShowFolder(strFolder: string);
begin
ShellExecute(Application.Handle,PChar('explore'),PChar(strFolder),nil,nil,SW_SHOWNORMAL);
end;
是否也可以通过此方式传递文件名(完整文件名路径或仅名称和扩展名),并在Windows资源管理器中打开该文件夹,但该文件夹也已突出显示/选中了?我要转到的位置有很多文件,然后需要在Windows中操作该文件。
Is there a way to also pass this a file name (either the full file name path or just the name + extension) and have the folder open in Windows Explorer but also be highlighted/selected? The location I am going to has many files and I need to then manipulate that file in Windows.
推荐答案
是的,您可以使用 / select
标志,当您调用 explorer.exe
:
Yes, you can use the /select
flag when you call explorer.exe
:
ShellExecute(0, nil, 'explorer.exe', '/select,C:\WINDOWS\explorer.exe', nil,
SW_SHOWNORMAL)
更花哨(也许也更可靠)的方法(使用ShellAPI,ShlObj
):
A somewhat more fancy (and perhaps also more reliable) approach (uses ShellAPI, ShlObj
):
const
OFASI_EDIT = $0001;
OFASI_OPENDESKTOP = $0002;
{$IFDEF UNICODE}
function ILCreateFromPath(pszPath: PChar): PItemIDList stdcall; external shell32
name 'ILCreateFromPathW';
{$ELSE}
function ILCreateFromPath(pszPath: PChar): PItemIDList stdcall; external shell32
name 'ILCreateFromPathA';
{$ENDIF}
procedure ILFree(pidl: PItemIDList) stdcall; external shell32;
function SHOpenFolderAndSelectItems(pidlFolder: PItemIDList; cidl: Cardinal;
apidl: pointer; dwFlags: DWORD): HRESULT; stdcall; external shell32;
function OpenFolderAndSelectFile(const FileName: string): boolean;
var
IIDL: PItemIDList;
begin
result := false;
IIDL := ILCreateFromPath(PChar(FileName));
if IIDL <> nil then
try
result := SHOpenFolderAndSelectItems(IIDL, 0, nil, 0) = S_OK;
finally
ILFree(IIDL);
end;
end;
这篇关于打开Windows资源管理器目录,选择一个特定文件(在Delphi中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!