Einzelnen Beitrag anzeigen

Schokohase
(Gast)

n/a Beiträge
 
#9

AW: Welche ist die aktuell gültige API um Systempfade zu bekomme?

  Alt 24. Feb 2019, 11:29
Ich lasse mich da gerne von .Net inspirieren*

System.Environment.GetFolderPath (referencesource)

und dann käme ich z.B. auf
Delphi-Quellcode:
interface

{$SCOPEDENUMS ON}

const
  CSIDL_FLAG_CREATE = $8000;
  CSIDL_FLAG_DONT_VERIFY = $4000;
  CSIDL_ADMINTOOLS = $0030;
  CSIDL_PROFILE = $0028;

type
  TEnvironment = record
  public type
    TSpecialFolder = ( //
      AdminTools = CSIDL_ADMINTOOLS, //
      UserProfile = CSIDL_PROFILE);
    TSpecialFolderOption = ( //
      None = 0, //
      Create = CSIDL_FLAG_CREATE, //
      DoNotVerify = CSIDL_FLAG_DONT_VERIFY);
  private
    class function InternalGetFolder(AFolder: TSpecialFolder; AOption: TSpecialFolderOption): string; static;
  public
    class function GetFolderPath(AFolder: TSpecialFolder): string; overload; static;
    class function GetFolderPath(AFolder: TSpecialFolder; AOption: TSpecialFolderOption): string; overload; static;
  end;

implementation

uses
  Winapi.Windows,
  Winapi.SHFolder;

{ TEnvironment }

class function TEnvironment.GetFolderPath(AFolder: TSpecialFolder; AOption: TSpecialFolderOption): string;
begin
  Result := InternalGetFolder(AFolder, AOption);
end;

class function TEnvironment.InternalGetFolder(AFolder: TSpecialFolder; AOption: TSpecialFolderOption): string;
var
  LStr: array [0 .. MAX_PATH] of Char;
begin
  SetLastError(ERROR_SUCCESS);

  if SHGetFolderPath(0, Integer(AFolder) or Integer(AOption), 0, 0, @LStr) = S_OK then
    Result := LStr;
end;

class function TEnvironment.GetFolderPath(AFolder: TSpecialFolder): string;
begin
  Result := InternalGetFolder(AFolder, TSpecialFolderOption.None);
end;
Und der Aufruf ist sehr sprechend: TEnvironment.GetFolderPath(TEnvironment.TSpecialFolder.UserProfile); .

Und um auf wirklich alle SpecialFolder zuzugreifen muss man nur etwas Fleißarbeit investieren und TEnvironment.TSpecialFolder komplettieren.

* Was ich auch nicht verwerflich finden, gerade weil Emba das auch sehr gerne macht, allerdings hat man das Gefühl, dass die dabei meistens schielen.

Geändert von Schokohase (24. Feb 2019 um 11:33 Uhr)
  Mit Zitat antworten Zitat