// Es wird der Windows-Explorer mit dem Ordner geöffnet, in dem sich
// eine angegebene Datei befindet, wobei die Datei selektiert wird.


// Getestet mit D2010 unter Win7
 

// Variante 1

procedure OpenExplorerAndSelectFile(FileName: TFileName); 
begin 
  shellexecute(0, 'open', pchar('Explorer'), 
    pchar('/e,/select, "' + FileName + '"'), nil, SW_SHOWNA); 
end;
 
// Variante 2
// (Stammt nicht von mir)
// Scrollt die Datei auch noch ins Sichtfeld
uses Winapi.ShellApi, Winapi.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; 
 
// Beispielaufruf 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  OpenFolderAndSelectFile('D:\Bilder\Zylinder.jpg'); 
end; 


 

Zugriffe seit 6.9.2001 auf Delphi-Ecke