antiblock
Cyphriun
  • Chatbox

    You don't have permission to chat.
    Load More
Sign in to follow this  
V¡®u§

Trabalhando com o registro do windows no Delphi

1 post in this topic

O registro do windows é onde todas as configurações são guardadas, tanto do próprio windows quanto da maioria dos outros softwares." Em que se aplica a utilização do registro? Se aplica simplismente a guardar informações e alterar informações de configurações do sistema operacional.

Agora vamos por a mão na massa ou melhor no teclado.

Escrevendo e lendo Strings no registro

Escrevendo

var

Reg: TRegistry;

begin

Reg := TRegistry.Create;

try

{ Define a chave-raiz do registro }

Reg.RootKey := HKEY_CURRENT_USER;

{ Abre a chave (path). Se não existir, cria e abre. }

Reg.OpenKey('MeuProgramaProgmaster\Configuração', true);

{ Escreve uma string }

Reg.WriteString('Nome', 'teste......');

finally

Reg.Free;

end;

end;

Lendo

var

Reg: TRegistry;

nome : string;

begin

Reg := TRegistry.Create;

try

Reg.RootKey := HKEY_CURRENT_USER;

if Reg.KeyExists('MeuProgramaProgmaster\Configuração') then

begin

Reg.OpenKey('MeuProgramaprogmaster\Configuração', false);

if Reg.ValueExists('Nome') then

nome := Reg.ReadString('Nome')

else

ShowMessage('Não existe valor com o nome "Nome"');

end else

ShowMessage('Não existe a chave no registro');

finally

Reg.Free;

end;

end;

Escrevendo e lendo Inteiros no registro

Escrevendo

var

Reg: TRegistry;

begin

Reg := TRegistry.Create;

try

{ Define a chave-raiz do registro }

Reg.RootKey := HKEY_CURRENT_USER;

{ Abre a chave (path). Se não existir, cria e abre. }

Reg.OpenKey('MeuProgramaProgmaster\Configuração', true);

{ Escreve um Inteiro/integer }

Reg.WriteInteger('Numero', 76);

finally

Reg.Free;

end;

end;

Lendo

var

Reg: TRegistry;

numero : integer;

begin

Reg := TRegistry.Create;

try

Reg.RootKey := HKEY_CURRENT_USER;

if Reg.KeyExists('MeuProgramaProgmaster\Configuração') then

begin

Reg.OpenKey('MeuProgramaprogmaster\Configuração', false);

if Reg.ValueExists('Numero') then

numero := Reg.ReadInteger('Numero')

else

ShowMessage('Não existe valor com o nome "Numero"');

end else

ShowMessage('Não existe a chave no registro');

finally

Reg.Free;

end;

end;

Escrevendo e lendo dados binarios no registro

Escrevendo

var

Reg: TRegistry;

Ficha: TFicha;

begin

{ Coloca alguns dados na variável Ficha }

Ficha.Codigo := 1;

Ficha.Nome := 'Jefferson Farias';

Ficha.DataCadastro := StrToDate(FormatDateTime('dd/m/yyyy',now));

Reg := TRegistry.Create;

try

{ Define a chave-raiz do registro }

Reg.RootKey := HKEY_CURRENT_USER;

{ Abre uma chave (path). Se não existir cria e abre. }

Reg.OpenKey('Cadastro\Pessoas\', true);

{ Grava os dados (o registro) }

Reg.WriteBinaryData('Dados', Ficha, SizeOf(Ficha));

finally

Reg.Free;

end;

end;

Lendo

var

Reg: TRegistry;

Ficha: TFicha;

codigo : integer;

nome : string;

data : TDate;

begin

Reg := TRegistry.Create;

try

{ Define a chave-raiz do registro }

Reg.RootKey := HKEY_CURRENT_USER;

{ Se existir a chave (path)... }

if Reg.KeyExists('Cadastro\Pessoas') then

begin

{ Abre a chave (path) }

Reg.OpenKey('Cadastro\Pessoas', false);

{ Se existir o valor... }

if Reg.ValueExists('Dados') then

begin

{ Lê os dados }

Reg.ReadBinaryData('Dados', Ficha, SizeOf(Ficha));

codigo := Ficha.Codigo;

nome := Ficha.Nome;

Data := Ficha.DataCadastro;

end else

ShowMessage('Valor não existe no registro.')

end else

ShowMessage('Chave (path) não existe no registro.');

finally

Reg.Free;

end;

end;

Colocando sua aplicação para auto iniciar com o windows

Insira o seguinte código em um tbutton ou em um TTimer

var

Reg: TRegistry;

S: string;

begin

Reg := TRegistry.Create;

S:=ExtractFileDir(Application.ExeName)+'\'+ExtractFileName(Application.ExeName);

Reg.rootkey:=HKEY_LOCAL_MACHINE;

Reg.Openkey('SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\RUN',false);

Reg.WriteString('Nome_Sistema',S);

Reg.closekey;

Reg.Free;

Observação: Para usar a classe TRegistry é nessessário adicionar Registry na cláusula Uses

Share this post


Link to post
Share on other sites
antiblock
Rodnia | Alpha & Omega

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this