Sunday, May 28, 2017

Delphi File Basics

function Read_ini(junk:string):boolean;

var
  folderName : string;
  fileName : string;
  myFile   : TextFile;
  data     : string;
begin

     //showmessage (Board.FileName);
     //showmessage (PCBServer.GetCurrentPCBBoard.FileName);
     //showmessage (ExtractFilePath(Application.ExeName));
     //showmessage (ClientAPI_SpecialFolder_AltiumApplicationData);

     // For ini file Saved in Project Folder
     FileName := (ExtractFilePath(Board.FileName))+ 'Designators.ini';

     // Global ini file
     if Not FileExists(FileName) then
     begin
       // Try to open a text file for writing to
       folderName := ExtractFilePath(ClientAPI_SpecialFolder_AltiumApplicationData) + 'Scripts\';
       CreateDir (folderName);
       FileName := folderName + 'Designators.ini';
     end;

     //showmessage (FileName);

     if FileExists(fileName) then
     begin
         AssignFile(myFile, fileName);

         // Reopen the file in read mode
         Reset(myFile);

         // Read the file contents
         //while not Eof(myFile) do
         //begin

         //ShowMessage(data);

         if not Eof(myFile) then ReadLn(myFile, data);
            EditMinHeight.Text:= data;

         if not Eof(myFile) then ReadLn(myFile, data);
            EditMaxHeight.Text:= data;

         if not Eof(myFile) then ReadLn(myFile, data);
            EditCommentsTop.Text:= data;

         if not Eof(myFile) then ReadLn(myFile, data);
            EditCommentsBot.Text:= data;

         if not Eof(myFile) then ReadLn(myFile, data);
            EditTestPointsTop.Text:= data;

         if not Eof(myFile) then ReadLn(myFile, data);
            EditTestPointsBot.Text:= data;

         //end;

         CloseFile(myFile);
         result := True
     end;

     // Debug Code
     // Now see if the file exists
     // if FileExists(fileName)
     // then ShowMessage(fileName+' exists OK')
     // else ShowMessage(fileName+' does not exist');

     // Delete the file and look again
     // DeleteFile(fileName);
     // if FileExists(fileName)
     // then ShowMessage(fileName+' still exists!')
     // else ShowMessage(fileName+' no longer exists');

end;


function Write_ini(junk:string):boolean;

var
  folderName : string;
  fileName : string;
  myFile : TextFile;

begin

  // Open a text file for writing to

    // For .ini files Saved in Project Folder
    fileName := (ExtractFilePath(Board.FileName)) + 'Designators.ini';
    AssignFile(myFile, fileName);
    ReWrite(myFile);
      Write(myFile, EditMinHeight.Text + #13#10);
      Write(myFile, EditMaxHeight.Text + #13#10);
      Write(myFile, EditCommentsTop.Text + #13#10);
      Write(myFile, EditCommentsBot.Text + #13#10);
      Write(myFile, EditTestPointsTop.Text + #13#10);
      Write(myFile, EditTestPointsBot.Text + #13#10);
    CloseFile(myFile);

    // Global ini file
    if Not FileExists(fileName) then
    begin
      folderName := ExtractFilePath(ClientAPI_SpecialFolder_AltiumApplicationData) + 'Scripts\';
      CreateDir (folderName);
      fileName := folderName + 'Designators.ini';
      AssignFile(myFile, fileName);
      //DeleteFile(fileName);
      ReWrite(myFile);
      Write(myFile, EditMinHeight.Text + #13#10);
      Write(myFile, EditMaxHeight.Text + #13#10);
      Write(myFile, EditCommentsTop.Text + #13#10);
      Write(myFile, EditCommentsBot.Text + #13#10);
      Write(myFile, EditTestPointsTop.Text + #13#10);
      Write(myFile, EditTestPointsBot.Text + #13#10);
      CloseFile(myFile);
    end;

end;


Debug Code

     // See if the file exists
     // if FileExists(fileName)
     // then ShowMessage(fileName+' exists OK')
     // else ShowMessage(fileName+' does not exist');

     // Delete the file and look again
     // DeleteFile(fileName);
     // if FileExists(fileName)
     // then ShowMessage(fileName+' still exists!')

     // else ShowMessage(fileName+' no longer exists');

Related Links:
Delphi Basics : Files

No comments:

Post a Comment