例:列出注册表"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run"中的所有键值到界面上或文本中;并把自己做的这个程序设置为开机启动。(写到前面那个注册表路径上就生效了)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, registry, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Show: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
m_Reg: Tregistry;
m_KeyList: TStrings;
i, j: Integer;
AppPath:string;
begin
Show.Lines.Clear;
m_Reg := TRegistry.Create;
m_KeyList := TStringList.Create;
m_Reg.RootKey := HKEY_CURRENT_USER;
//false:不存在的话 也不创建
if m_Reg.OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Run', false) then
begin
//获取所有值存入list中
m_Reg.GetValueNames(m_KeyList);
for i := 0 to m_KeyList.Count - 1 do
begin
Show.Lines.Add(m_KeyList[i] + ': ' + m_Reg.ReadString(m_KeyList[i]));
end;
//获取当前可执行文件的路径
APPpath:=ExtractFilePath(Application.ExeName);
m_Reg.WriteString('Day01Pro',AppPath);
end
else
begin
ShowMessage('open Run err!');
end;
m_Reg.Free;
m_KeyList.Free;
end;
end.