Recognize File . INI / Redo File . INI in case of file deletion

Asked

Viewed 854 times

0

Follow below the code:

procedure TfrmSelection.FormActivate(Sender: TObject);
  var
    ArqIni: TIniFile;
  begin
    ArqIni := ArqIni.Create('C:\sga\saga.ini');
    try
    ZConnection1.Connected := false;
    ZConnection1.HostName := ArqIni.ReadString('BASEDADOS', 'PathSec', '');
    ZConnection1.Database := ArqIni.ReadString('BASEDADOS','Base', '');
    ZConnection1.Password := ArqIni.ReadString('BASEDADOS','Chave', '');
    except
    on E:Exception do MessageDlg('Erro ao conectar!'#13'Erro: '+ e.Message, mtError, [mbOK], 0);
    end;
  end;

However, I believe that this is the code that is giving error of Access Violation.

My question is: How would I improve the code to the point of recreating the folder, the file and the contents of it in case of deletion of the file. INI?

1 answer

1


I made it here this way and it worked:

var
  arquivo : TIniFile;
  existeArquivo : BOOL;
begin
  if not DirectoryExists('C:\sga') then
  begin
    if not CreateDir('C:\sga') then
    begin
      ShowMessage('Erro ao tentar criar o diretório!');
    end;
  end;
  existeArquivo := True;
  // Verifica se existe o arquivo
  if not FileExists('C:\sga\saga.ini') then
    existeArquivo := False;

  arquivo := TIniFile.Create('C:\sga\saga.ini');

  // Gravar valores
  if not existeArquivo then
  begin
    arquivo.WriteString('BASEDADOS', 'PathSec', 'PathSecTestes');
    arquivo.WriteString('BASEDADOS', 'Base', 'BaseTestes');
    arquivo.WriteString('BASEDADOS', 'Chave', 'ChaveTestes');
  end;

  // Lendo valores
  Edit1.Text := arquivo.ReadString('BASEDADOS', 'PathSec', '');
  • Thanks Marlon! Very Good! ;]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.