Recognize .INI file / redo .INI file in case of file deletion

0

The following is the code below:

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 this is the code that is giving Access Violation error.

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

    
asked by anonymous 08.05.2014 / 15:44

1 answer

1

I did it 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', '');
    
08.05.2014 / 16:04