Start automatic program code loading (Registry Implementation)

Source: Internet
Author: User

Unit mainunit;

Interface

Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls, buttons, registry;

Type
Tform1 = Class (tform)
Groupbox1: tgroupbox;
Radiobutton1: tradiobutton;
Checkbox1: tcheckbox;
Radiobutton2: tradiobutton;
Edit1: tedit;
Label1: tlabel;
Label2: tlabel;
Edit2: tedit;
Button1: tbutton;
Bitbtn1: tbitbtn;
Opendialog1: topendialog;
Procedure button1click (Sender: tobject );
Procedure FormCreate (Sender: TObject );
Procedure BitBtn1Click (Sender: TObject );
Procedure RadioButton2Click (Sender: TObject );
Procedure RadioButton1Click (Sender: TObject );
Private
{Private declarations}
Public
Procedure WriteToCurUser;
Procedure WriteToAllUserAsServ;
Procedure WriteToAllUserAsApp;
{Public declarations}
End;

Var
Form1: TForm1;

Implementation

{$ R *. dfm}

Procedure TForm1.WriteToCurUser;
Var
Reg: TRegistry;
Begin
Reg: = TRegistry. Create;
Try
Reg. RootKey: = HKEY_CURRENT_USER;
If Reg. OpenKey ('\ SOFTWARE \ MICROSOFT \ Windows \ CurrentVersion \ run', True) then
Begin
Reg. WriteString (Edit2.Text, Edit1.Text );
End;
Finally
Reg. CloseKey;
Reg. Free;
End;
End;

Procedure TForm1.WriteToAllUserAsApp;
Var
Reg: TRegistry;
Begin
Reg: = TRegistry. Create;
Try
Reg. RootKey: = HKEY_Local_Machine;
If Reg. OpenKey ('\ SOFTWARE \ MICROSOFT \ Windows \ CurrentVersion \ run', True) then
Begin
Reg. WriteString (Edit2.Text, Edit1.Text );
End;
Finally
Reg. CloseKey;
Reg. Free;
End;
End;

Procedure TForm1.WriteToAllUserAsServ;
Var
Reg: TRegistry;
Begin
Reg: = TRegistry. Create;
Try
Reg. RootKey: = HKEY_Local_Machine;
If Reg. OpenKey ('\ SOFTWARE \ MICROSOFT \ Windows \ CurrentVersion \ runservice', True) then
Begin
Reg. WriteString (Edit2.Text, Edit1.Text );
End;
Finally
Reg. CloseKey;
Reg. Free;
End;
End;

Procedure TForm1.Button1Click (Sender: TObject );
Begin
If (Edit1.Text = '') or (Edit2.Text ='') then
Begin
ShowMessage ('select the application and specify the startup Item name, and then try the write operation again! ');
If (Edit1.Text = '') then
BitBtn1.Click
Else
Edit2.SetFocus;
End
Else
Begin
If RadioButton1.Checked then
Begin
WriteToCurUser;
ShowMessage ('the startup Item is successfully written to the registry! ');
End
Else
Begin
If CheckBox1.Checked then
Begin
WriteToAllUserAsServ;
ShowMessage ('the startup Item is successfully written to the registry! ');
End
Else
Begin
Writetoalluserasapp;
Showmessage ('the startup Item is successfully written to the registry! ');
End;
End;
End;
End;

Procedure tform1.formcreate (Sender: tobject );
Begin
Opendialog1.initialdir: = extractfiledir (application. exename );
Opendialog1.filename: = '';
Checkbox1.enabled: = false;
End;

Procedure tform1.bitbtn1click (Sender: tobject );
Begin
With opendialog1 do
Begin
If execute then
Edit1.text: = opendialog1.filename;
End;
End;

Procedure tform1.radiobutton2click (Sender: tobject );
Begin
Checkbox1.enabled: = true;
End;

Procedure tform1.radiobutton1click (Sender: tobject );
Begin
Checkbox1.enabled: = false;
End;

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.