Delphi-PRAXiS
Seite 3 von 6     123 45     Letzte »    

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Funktion erstellen (https://www.delphipraxis.net/26182-funktion-erstellen.html)

Rackergen2 19. Jul 2004 19:56

Re: Funktion erstellen
 
Delphi-Quellcode:
function TForm1.createautorunfile(app : String; icon : String; destfile : String):boolean;
var
 sl : TStringList;
begin
  result:=true;
  try
    sl:=TStringList.create;
    sl.add('[autorun]');
    sl.add('OPEN=' + app);
    sl.add('ICON=' + icon);
    if copy(destfile,length(destfile-4),4)<>'.inf' then destfile:=destfile+'.inf';
    sl.SaveToFile(destfile);
    s1.free;
  except
    result:=false;
  end;
end;

end.
So besser?

Matze 19. Jul 2004 20:44

Re: Funktion erstellen
 
Delphi-Quellcode:
if copy(destfile,length(destfile-4),4)<>'.inf' then destfile:=destfile+'.inf';
Gefällt mir so besser:

Delphi-Quellcode:
if not Pos('.inf', destfile) > 0 then destfile := destfile + '.inf';
Aber das spielt ja auch keine große Rolle. ;)

Edit:

Unde du solltest es irgendwie so machen:

Delphi-Quellcode:
...

try

  ...

finally
  sl.Free;

p0w3r5tr34m3r 19. Jul 2004 20:56

Re: Funktion erstellen
 
geht das nich auf ohne stringliste ??? inifiles ???

der aufbau is ja eigentlich der selbe oder ?

S2B 19. Jul 2004 21:24

Re: Funktion erstellen
 
Klar geht das mit ini-Dateien! 8)

Rackergen2 19. Jul 2004 22:49

Re: Funktion erstellen
 
Zitat:

Zitat von Matze
Delphi-Quellcode:
if copy(destfile,length(destfile-4),4)<>'.inf' then destfile:=destfile+'.inf';
Gefällt mir so besser:

Delphi-Quellcode:
if not Pos('.inf', destfile) > 0 then destfile := destfile + '.inf';
Aber das spielt ja auch keine große Rolle. ;)

Denkste! Lass mal die Datei so heißen: 'C:\test\test.inf.txt' Du musst schon dafür sorgen, dass das .inf hinten ist... höchstens so:

Delphi-Quellcode:
if Pos('.inf', destfile)<length(destfile)-4 then destfile:=destfile+'.inf';


Delphi-Quellcode:
function TForm1.createautorunfile(app : String; icon : String; destfile : String):boolean;
var
sl : TStringList;
begin
  result:=true;
  sl:=TStringList.create;
  sl.add('[autorun]');
  sl.add('OPEN=' + app);
  sl.add('ICON=' + icon);
  if copy(destfile,length(destfile-4),4)<>'.inf' then destfile:=destfile+'.inf';

  try
    sl.SaveToFile(destfile);
  except
    result:=false;
  end;

  s1.free;
end;

Matze 20. Jul 2004 08:27

Re: Funktion erstellen
 
Hmm, stimmt, an so einen "Sonderfall" habe ich nicht gedacht.

Nur sollte man noch die Resourcenschutzblöcke einbauen:

Delphi-Quellcode:
function TForm1.CreateAutorunFile(app, icon, destfile: String): boolean;
var
  sl: TStringList;
begin
  result := true;
  sl := TStringList.create;
  try
    sl.add('[autorun]');
    sl.add('OPEN=' + app);
    sl.add('ICON=' + icon);
    if copy(destfile, length(destfile) - 4, 4) <> '.inf' then destfile := destfile + '.inf';
    try
      sl.SaveToFile(destfile);
    except
      result:=false;
    end;
  finally
    sl.free;
  end;
end;
Ich hab auch noch den ein oder anderen Fehler verbessert, dein Code lies sich nicht kompilieren. ;)

Eines darf man nicht vergessen:
Wenn ich nun den Pfad und das Icon angebe, so habe ich den kompletten Pfad beider in der Ini, was natürlich Mist ist.
Nacher befinden sich die Anwendung und das Icon (im Normalfall) auf der CD, somit stimmen die Pfade nicht mehr!
Man musste Icon und Anwendung in den Projektordner geben und dann die relativen Pfade (Anwendung + ggf. Ordnername) in die inf-Datei eintragen.

Rackergen2 20. Jul 2004 10:45

Re: Funktion erstellen
 
Zitat:

Zitat von Matze
Delphi-Quellcode:
...
  sl.add('[autorun]');
  sl.add('OPEN=' + app);
  sl.add('ICON=' + icon);
  if copy(destfile, length(destfile) - 4, 4) <> '.inf' then destfile := destfile + '.inf';
  ...

Warum hast du das in einer try-Klausel? Daran kann doch nix schief gehen...

Delphi-Quellcode:
function TForm1.CreateAutorunFile(app, icon, destfile: String): boolean;
var
  sl: TStringList;
begin
  result:=true;
  sl:=TStringList.create;
  sl.add('[autorun]');
  sl.add('OPEN='+app);
  sl.add('ICON='+icon);
  if copy(destfile, length(destfile)-4, 4)<>'.inf' then destfile:=destfile+'.inf';

  try
    sl.SaveToFile(destfile);
  except
    result:=false;
  end;

  sl.free;
end;
Und "den einen oder anderen Fehler" kann ich auch nicht erkennen, nur dass du da noch Leerzeichen reingeklatscht hast...

EDIT: und das -4, aber wo ist "der andere Fehler"? ;)

PS: Wie ich diese ganze Leerzeichensetzung hasse...

mirage228 20. Jul 2004 11:15

Re: Funktion erstellen
 
Hi,

wie wärs hiermit?

Delphi-Quellcode:
function TForm1.CreateAutorunFile(const App, Icon, DestFile: String): Boolean;
var
  sl: TStringList;
begin
  Result := False;
  sl := TStringList.create;
  try
    sl.add('[autorun]');
    sl.add('OPEN=' + App);
    sl.add('ICON=' + Icon);
    sl.SaveToFile(ChangeFileExt(DestFile, '.inf'));
    Result := True;
  finally
    sl.Free;
  end;
end;
mfG
mirage228

Matze 20. Jul 2004 11:21

Re: Funktion erstellen
 
@Rackergen2: Jo, des war glaub der einzige Fehler. :wink:

@Mirage: Cool! :thumb:

Rackergen2 20. Jul 2004 15:31

Re: Funktion erstellen
 
Zitat:

Zitat von mirage228
wie wärs hiermit?

Delphi-Quellcode:
function TForm1.CreateAutorunFile(const App, Icon, DestFile: String): Boolean;
var
  sl: TStringList;
begin
  Result := False;
  sl := TStringList.create;
  try
    sl.add('[autorun]');
    sl.add('OPEN=' + App);
    sl.add('ICON=' + Icon);
    sl.SaveToFile(ChangeFileExt(DestFile, '.inf'));
    Result := True;
  finally
    sl.Free;
  end;
end;

Das ist doch Schwachsinn. Was sollen die ersten 3 Zeilen in einem try? Wenn, dann so:
Delphi-Quellcode:
function TForm1.CreateAutorunFile(const App, Icon, DestFile: String): Boolean;
var
  sl: TStringList;
begin
  Result := False;
  sl := TStringList.create;
  sl.add('[autorun]');
  sl.add('OPEN=' + App);
  sl.add('ICON=' + Icon);
  try
    sl.SaveToFile(ChangeFileExt(DestFile, '.inf'));
    Result := True;
  finally
    sl.Free;
  end;
end;
Aber das käme sowas von das gleiche raus... dann doch lieber meine Funtion oben... ;)


Alle Zeitangaben in WEZ +1. Es ist jetzt 03:23 Uhr.
Seite 3 von 6     123 45     Letzte »    

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz