使用Delphi windows api获取文件信息

时间:2017-01-24 11:08:05

标签: delphi winapi delphi-xe

如果文件是文件夹,可以从PItemIDList使用Delphi和Windows API吗?有了这段代码,我就可以得到唯一的文件名。

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;

由于

1 个答案:

答案 0 :(得分:4)

对于相对PIDL,您可以获取PIDL父文件夹的IShellFolder接口,然后将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;
    
  • 使用SHGFI_PIDL标志将PIDL传递给SHGetFileInfo()。启用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接口,然后拨打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;