[编辑]
function PathFromShellItem(AShellItem: IShellItem): string;
{Helper function to get path from ShellItem}
var
hr: HRESULT;
iPath: PWideChar;
begin
hr := AShellItem.GetDisplayName(SIGDN_FILESYSPATH, iPath);
if hr = 0 then
Result := iPath
else
Result := '';
end;
下面的代码尝试从 IShellItem 获取文件夹名和文件名。文件名 (iName) 获取正确,但文件夹名称 (iFolder) 始终为“”。为什么 pfd.GetFolder 不返回文件夹名称?我是否试图以错误的方式获取文件夹名称?
function TFileDialogEvent.OnSelectionChange(const pfd: IFileDialog): HResult; stdcall;
{Handle the OnSelectionChange event and fill labels with information}
var
ShellItem: IShellItem;
iFilename: string;
iFolder: string;
iName: PWideChar;
begin
if pfd.GetFolder(ShellItem) = S_OK then
iFolder := PathFromShellItem(ShellItem);
OleCheck(pfd.GetFileName(iName));
{Set the filepath}
if DirectoryExists(iFolder) then
iFilename := IncludeTrailingPathDelimiter(iFolder) + string(iName);
end;