Delphi SET开机自启动项

原理

其实就是对注册表进行修改

直接上代码!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
procedure SetAutorun(aProgTitle,aCmdLine: string; aRunOnce: boolean );
var
hKey: string;
hReg: TRegIniFile;
begin
if aRunOnce then
//程序只自动运行一次
hKey := 'Once'
else //aProgTitle:=applicatian.Title /aCmdLine:=application.ExeName
hKey := '';
hReg := TRegIniFile.Create('');
//TregIniFile类的对象需要创建
hReg.RootKey := HKEY_LOCAL_MACHINE;//其实就是往注册表的HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Windows\CurrentVersion\Run 中添加
//设置根键
hReg.WriteString('Software\Microsoft\Windows\CurrentVersion\Run'
+ hKey + #0,
aProgTitle,
//程序名称,可以为自定义值
aCmdLine );
//命令行数据,必须为该程序的绝对路径+程序完整名称
hReg.destroy;
//释放创建的hReg
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SetAutorun(Application.Title,application.ExeName,false);
end;
//////////////////////////
uses Registry;
var
Reg:TRegistry;
Str:string;
begin
Str:=ExtractFileDir(Application.ExeName)+ '程序.exe ';
Reg:=Tregistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey( 'Software\Microsoft\Windows\CurrentVersion\Run ',False);
Reg.WriteString( '名称 ',str);
Reg.CloseKey;
Reg.Free;
end;

热评文章