Olá galera, como todos sabemos, a utilização do registro do windows e de arquivos INI é bem grande, considerando que as vezes não vale a pena colocar tudo no banco de dados, como por exemplo, algumas configurações por terminal e coisas do tipo. Sendo assim, segue algumas dicas sobre como simplificar o uso destes recursos.
//Arquivos INI
//Escreve um Valor String em uma Determinada Seção
procedure WriteIni(FileName, Section, Ident, Value: string);
begin
with TIniFile.Create('C:\Windows\Temp\' + FileName + '.ini') do
try
WriteString(Section, Ident, Value);
finally
Free;
end;
end;
//Lê um Valor String de uma Determinada Seção
function ReadIni(const FileName: string; const Section: string; const Ident: string): string;
begin
with TIniFile.Create('C:\Windows\Temp\' + FileName + '.ini') do
try
Result := ReadString(Section, Ident, EmptyStr);
finally
Free;
end;
end;
//Registro do Windows
//-------------------
//Escreve um Valor String em uma Determinada Chave de uma Seção
procedure WriteRegKey(Section, Key, Value: string);
begin
with TRegistry.Create do
begin
RootKey := DWORD($80000002);
OpenKey('\Software\' + 'NomeDoPrograma' + '\' + Section, True);
WriteString(Key, Value);
Free;
end;
end;
//Escreve um Valor String em uma Determinada Chave de uma Sub-Seção
procedure WriteRegKeySub(Section, SubSection, Key, Value: string);
begin
with TRegistry.Create do
begin
RootKey := DWORD($80000002);
OpenKey('\Software\' + 'NomeDoPrograma' + '\' + Section + '\' + SubSection, True);
WriteString(Key, Value);
Free;
end;
end;
//Lê um Valor String de uma Determinada Chave de uma Seção
function ReadRegKey(Section, Key: string): string;
begin
with TRegistry.Create do
begin
RootKey := DWORD($80000002);
OpenKey('\Software\' + 'NomeDoPrograma' + '\' + Section, True);
Result := ReadString(Key);
Free;
end;
end;
//Lê um Valor String de uma Determinada Chave de uma Sub-Seção
function ReadRegKeySub(Section, SubSection, Key: string): string;
begin
with TRegistry.Create do
begin
RootKey := DWORD($80000002);
OpenKey('\Software\' + 'NomeDoPrograma' + '\' + Section + '\' + SubSection, True);
Result := ReadString(Key);
Free;
end;
end;
Depois das funções declaradas, para gravar e ler uma chave em um arquivo Ini,
basta fazer:
//para gravar
WriteIni('Config', 'Alertas', 'Backup', 'S');
//para ler
if ReadIni('Config', 'Alertas', 'Backup') = 'S' then
ShowMessage('Faça o Backup!');
Já com o Registro do Windows:
//para gravar
WriteRegKey('Alertas', 'Backup', 'S');
//para ler
if ReadRegKey('Alertas', 'Backup') = 'S' then
ShowMessage('Faça o Backup!');
Valeu amigos, espero que gostem! Um grande abraço...
Rafael
Contato:
rafakwolf@bol.com.br
|