gpt4 book ai didi

delphi - 使用Delphi windows api获取文件信息

转载 作者:行者123 更新时间:2023-12-01 19:21:31 24 4
gpt4 key购买 nike

使用 Delphi 和 Windows API 可以从 PItemIDList 获取文件是否是文件夹?通过这段代码,我可以获得文件的唯一名称。

procedure TMain.FolderMonitorFileChange(aSender: TObject; aPIDL: PItemIDList);
var
FileInfo : SHFILEINFOW;
begin
SHGetFileInfo(LPCTSTR(aPIDL), 0 , FileInfo, SizeOf(FileInfo), SHGFI_PIDL or SHGFI_DISPLAYNAME or SHGFI_TYPENAME or SHGFI_ATTRIBUTES);
ShowMessage('File change notification: ' + FileInfo.szDisplayName + ' ' + FileInfo.szTypeName );
end;

谢谢

最佳答案

对于相对PIDL,您可以获得IShellFolder PIDL的父文件夹的接口(interface),然后将PIDL传递给IShellFolder::GetAttributesOf()方法。

function IsFolder(Parent: IShellFolder; aChildPIDL: PItemIDList): Boolean;
var
Attrs: SFGAOF;
begin
Result := Succeeded(Parent.GetAttributesOf(1, @aChildPidl, @Attrs))
and (Attrs and SFGAO_FOLDER <> 0);
end;

对于绝对 PIDL,您有几种不同的选择:

  • 将 PIDL 传递给 SHBindToParent()将其转换为相对 PIDL 并检索其父文件夹的 IShellFolder,然后调用 IShellFolder::GetAttributesOf()

    function IsFolder(aPIDL: PItemIDList): Boolean;
    var
    Parent: IShellFolder;
    Child: PItemIDList;
    Attrs: SFGAOF;
    begin
    Result := Succeeded(SHBindToParent(aPidl, IShellFolder, @Parent, @Child))
    and Succeeded(Parent.GetAttributesOf(1, @Child, @Attrs))
    and (Attrs and SFGAO_FOLDER <> 0);
    end;
  • 将 PIDL 传递给 SHGetFileInfo()使用 SHGFI_PIDL 标志。启用 SHGFI_ATTRIBUTES 标志来请求项目的属性。

    function IsFolder(aPIDL: PItemIDList): Boolean;
    var
    FileInfo : SHFILEINFO;
    begin
    Result := (SHGetFileInfo(LPCTSTR(aPIDL), 0, @FileInfo, SizeOf(FileInfo), SHGFI_PIDL or SHGFI_ATTRIBUTES) <> 0)
    and (FileInfo.dwAttributes and SFGAO_FOLDER <> 0);
    end;
  • 将 PIDL 传递给 SHCreateItemFromIDList()检索 IShellItem接口(interface),然后调用IShellItem::GetAttributes() .

    function IsFolder(aPIDL: PItemIDList): Boolean;
    var
    Item: IShellItem;
    Attrs: SFGAOF;
    begin
    Result := Succeeded(SHCreateItemFromIDList(aPidl, IShellItem, @Item))
    and Succeeded(Item.GetAttributes(SFGAO_FOLDER, @Attrs))
    and (Attrs and SFGAO_FOLDER <> 0);
    end;

关于delphi - 使用Delphi windows api获取文件信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826654/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com