Compile a program that runs automatically with Windows Startup
By setting and reading the registry, we can implement the functions that our applications can execute with Windows startup. The following is the implementation code:
Form1.vb
Imports Microsoft. Win32.Registry
Public Class Form1
Inherits System. Windows. Forms. Form
# Region "code generated by Windows Form Designer"
Public Sub New ()
MyBase. New ()
'The call is required by the Windows Form Designer.
InitializeComponent ()
'Add any initialization after InitializeComponent () is called.
End Sub
'Form rewriting disposal to clear the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
Components. Dispose ()
End If
End If
MyBase. Dispose (disposing)
End Sub
'Windows forms designer required
Private components As System. ComponentModel. IContainer
'Note: The following procedure is required by the Windows Forms designer
'You can use the Windows Form Designer to modify this process.
'Do not use the code editor to modify it.
Friend WithEvents CheckBox1 As System. Windows. Forms. CheckBox
Friend WithEvents btnSave As System. Windows. Forms. Button
Private Sub InitializeComponent ()
Me. CheckBox1 = New System. Windows. Forms. CheckBox ()
Me. btnSave = New System. Windows. Forms. Button ()
Me. SuspendLayout ()
'
'Checkbox1
'
Me. checkbox1.backcolor = system. Drawing. systemcolors. Control
Me. checkbox1.checked = true
Me. checkbox1.checkstate = system. Windows. Forms. checkstate. Checked
Me. checkbox1.forecolor = system. Drawing. systemcolors. controltext
Me. checkbox1.imemode = system. Windows. Forms. imemode. nocontrol
Me. checkbox1.name = "checkbox1"
Me. checkbox1.righttoleft = system. Windows. Forms. righttoleft. Yes
Me. checkbox1.size = new system. Drawing. Size (142, 15)
Me. checkbox1.tabindex = 21
Me. checkbox1.text = "running upon Windows Startup"
Me. checkbox1.textalign = system. Drawing. contentalignment. middleright
'
'Btnsave
'
Me. btnsave. backcolor = system. Drawing. systemcolors. Control
Me. btnsave. font = new system. Drawing. Font ("", 10.0 !)
Me. btnsave. forecolor = system. Drawing. systemcolors. controltext
Me. btnsave. imemode = system. Windows. Forms. imemode. nocontrol
Me. btnsave. Location = new system. Drawing. Point (0, 16)
Me. btnsave. Name = "btnsave"
Me. btnsave. size = new system. Drawing. Size (144, 24)
Me. btnsave. tabindex = 32
Me. btnsave. Text = "Save settings"
'
'Form1
'
Me. AutoScaleBaseSize = New System. Drawing. Size (5, 13)
Me. ClientSize = New System. Drawing. Size (144, 40)
Me. Controls. AddRange (New System. Windows. Forms. Control () {Me. btnSave, Me. CheckBox1 })
Me. FormBorderStyle = System. Windows. Forms. FormBorderStyle. FixedToolWindow
Me. Name = "Form1"
Me. StartPosition = System. Windows. Forms. FormStartPosition. CenterScreen
Me. Text = "running upon Windows Startup"
Me. ResumeLayout (False)
End Sub
# End Region
Dim Reg As Microsoft. Win32.RegistryKey
Private Sub InitVar ()
If btnSave. Visible = True Then
End If
Reg = currentuser. opensubkey ("software/Microsoft/Windows/CurrentVersion/run", true)
If Reg. getvalue ("mengxianhui") <> "" then
Checkbox1.checked = true
Else
Checkbox1.checked = false
End if
End sub
Private sub savesettings ()
If checkbox1.checked = true then
Reg = currentuser. opensubkey ("software/Microsoft/Windows/CurrentVersion/run", true)
Reg. setvalue ("mengxianhui", application. executablepath)
Else
Reg = currentuser. opensubkey ("software/Microsoft/Windows/CurrentVersion/run", true)
Reg. setvalue ("mengxianhui ","")
End if
Initvar ()
MessageBox. Show ("You have already set it. Please restart your computer to see the effect. "," Prompt ",_
Messageboxbuttons. OK, messageboxicon. Information)
If checkbox1.checked = true then
Me. Dispose (true)
End if
End sub
Private sub btnsave_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btnsave. Click
Savesettings ()
End sub
End Class