![]() |
Dateioperationen mit SHFileOperation
Hi DPler,
diesen Beitrag habe ich zum ersten Mal am 1.11.2000 bei Delphi3000.com veröffentlicht. Nach und nach werde ich verschiedene meiner Beiträge hier einbringen. Die Prozedure DoFileWork, welche ich hier vorstelle, ermöglicht es Dateioperationen (kopieren, löschen, verschieben, ...) mit Hilfe der WinAPI Funktion SHFileOperation durchzuführen. Der Syntax
Delphi-Quellcode:
Ein Beispiel
function DoFileWork(
aWnd: HWND; aOperation: UINT; aFrom, aTo: TStrings; aFlags: FILEOP_FLAGS ): Integer;
Delphi-Quellcode:
Dieser Aufruf verschiebt alle Dateien und Verzeichnisse, welche in der StringListe strlFiles stehen in den Windows-Papierkorb.
DoFileWork(Self.Handle, FO_DELETE, strlFiles, nil, FOF_ALLOWUNDO);
Parameter:
Ergebnisse der Funktion:
Hinweis: Die Unit ShellAPI muss in der Uses Klausel stehen!
Delphi-Quellcode:
...:cat:...
function DoFileWork(
aWnd: HWND; aOperation: UINT; aFrom, aTo: TStrings; aFlags: FILEOP_FLAGS ): Integer; var I: Integer; FromPath, ToPath: string; SHFileOpStruct: TSHFileOpStruct; begin FromPath := ''; for I := 0 to aFrom.Count - 1 do FromPath := FromPath + aFrom.Strings[I] + #0; FromPath := FromPath + #0; if Assigned(aTo) then begin ToPath := ''; for I := 0 to aTo.Count - 1 do ToPath := ToPath + aTo.Strings[I] + #0; ToPath := ToPath + #0; if aTo.Count > 0 then aFlags := aFlags or FOF_MULTIDESTFILES; end; with SHFileOpStruct do begin Wnd := aWnd; wFunc := aOperation; pFrom := PChar(FromPath); if Assigned(aTo) then begin pTo := PChar(ToPath) end else begin // target available pTo := nil; end; // target not available fFlags := aFlags; end; // structure Result := SHFileOperation(SHFileOpStruct); end; |
Re: Dateioperationen mit SHFileOperation
Da obige Methode oft für Verwirrung sorgt, hier eine weitere Funktion, welche nicht auf StringListen, sondern auf einfachen Strings basiert. QuellDateiname (oder Verzeichnis ohne abschließenden Backslash) auf aFrom übergeben. Wenn nicht gelöscht wird, dann das Ziel an aTo übergeben, Operation (z.B. FO_MOVE) an aOperation übergeben und ab!
Delphi-Quellcode:
...:cat:...
function DoFileWork(aOperation: FILEOP_FLAGS; aFrom, aTo: AnsiString;
Flags: FILEOP_FLAGS): Integer; var FromPath, ToPath: AnsiString; SHFileOpStruct: TSHFileOpStruct; begin FromPath := aFrom + #0#0; ToPath := aTo + #0#0; with SHFileOpStruct do begin Wnd := 0; wFunc := aOperation; pFrom := PAnsiChar(FromPath); if ToPath <> '' then begin pTo := PAnsiChar(ToPath) end else begin // target available pTo := nil; end; // target not available fFlags := Flags; end; // structure Result := SHFileOperationA(SHFileOpStruct); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:04 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz