Thema: Delphi Guid erzeugen

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Guid erzeugen

  Alt 14. Mär 2016, 18:08
Du kannst ja mal das hier ausprobieren
Delphi-Quellcode:
program Project2;

{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.IOUtils,
  System.SysUtils;

type
  TArchiveFolder = class
  private
    FID : TGUID;
    FFullName : string;
    FFirstLoad: Boolean;
    function GetID: TGUID;
    function GetName: string;
    function GetIdFileName: string;
    function GetIsInitialized: Boolean;
    procedure EnsureFirstLoad;
  public
    property FullName : string read FFullName;
    property ID : TGUID read GetID;
    property IsInitialized: Boolean read GetIsInitialized;
    property name : string read GetName;
  public
    constructor Create( const Name: string );
  public
    procedure Init( CreateIfNotExists: Boolean = False );
    procedure Reload;
  end;

  { TArchiveFolder }

constructor TArchiveFolder.Create( const Name: string );
begin
  if TFile.Exists( name )
  then
    raise Exception.Create( 'Fehlermeldung' );

  inherited Create;
  FFullName := TPath.GetFullPath( name );
end;

procedure TArchiveFolder.EnsureFirstLoad;
begin
  if not FFirstLoad
  then
    begin
      Reload;
      FFirstLoad := True;
    end;
end;

function TArchiveFolder.GetID: TGUID;
begin
  EnsureFirstLoad;
  Result := FID;
end;

function TArchiveFolder.GetIdFileName: string;
begin
  Result := TPath.Combine( FFullName, '.folderid' );
end;

function TArchiveFolder.GetIsInitialized: Boolean;
begin
  EnsureFirstLoad;
  Result := FID <> TGUID.Empty;
end;

function TArchiveFolder.GetName: string;
begin
  Result := TPath.GetFileName( FFullName );
end;

procedure TArchiveFolder.Init( CreateIfNotExists: Boolean = False );
begin
  Reload;

  if IsInitialized
  then
    raise Exception.Create( 'Fehlermeldung' );

  if not TDirectory.Exists( FFullName )
  then
    begin
      if CreateIfNotExists
      then
        TDirectory.CreateDirectory( FFullName )
      else
        raise Exception.Create( 'Fehlermeldung' );
    end;

  TFile.WriteAllText( GetIdFileName( ), TGUID.NewGuid.ToString );

  Reload;
end;

procedure TArchiveFolder.Reload;
var
  lIdFileName: string;
begin
  lIdFileName := GetIdFileName( );
  if not TFile.Exists( lIdFileName )
  then
    FID := TGUID.Empty
  else
    FID := TGUID.Create( TFile.ReadAllText( lIdFileName ) );
end;

procedure Main;
var
  fld: TArchiveFolder;
begin
  fld := TArchiveFolder.Create( '.' );
  try
    if not fld.IsInitialized
    then
      begin
        WriteLn( 'Init ',fld.FullName );
        fld.Init( );
      end;

    WriteLn( fld.ID.ToString( ) );
  finally
    fld.Free;
  end;

  fld := TArchiveFolder.Create( '.' );
  try
    if not fld.IsInitialized
    then
      begin
        WriteLn( 'Init ',fld.FullName );
        fld.Init( );
      end;

    WriteLn( fld.ID.ToString( ) );
  finally
    fld.Free;
  end;

end;

begin
  try
    Main( );
  except
    on E: Exception do
      WriteLn( E.ClassName, ': ', E.Message );
  end;

  ReadLn;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat