编写随Windows启动运行的程序

来源:互联网 发布:惊艳的句子知乎 编辑:程序博客网 时间:2024/05/01 21:27

通过设置和读取注册表,可以实现我们的应用程序随Windows的启动即可执行的功能。下面就是实现的代码:

Form1.vb

Imports Microsoft.Win32.RegistryPublic Class Form1Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 "Public Sub New()MyBase.New()'该调用是 Windows 窗体设计器所必需的。InitializeComponent()'在 InitializeComponent() 调用之后添加任何初始化End Sub'窗体重写处置以清理组件列表。Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)If disposing ThenIf Not (components Is Nothing) Thencomponents.Dispose()End IfEnd IfMyBase.Dispose(disposing)End Sub'Windows 窗体设计器所必需的Private components As System.ComponentModel.IContainer'注意:以下过程是 Windows 窗体设计器所必需的'可以使用 Windows 窗体设计器修改此过程。'不要使用代码编辑器修改它。Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBoxFriend WithEvents btnSave As System.Windows.Forms.Button<System.Diagnostics.DebuggerStepThrough()> 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.ControlMe.CheckBox1.Checked = TrueMe.CheckBox1.CheckState = System.Windows.Forms.CheckState.CheckedMe.CheckBox1.ForeColor = System.Drawing.SystemColors.ControlTextMe.CheckBox1.ImeMode = System.Windows.Forms.ImeMode.NoControlMe.CheckBox1.Name = "CheckBox1"Me.CheckBox1.RightToLeft = System.Windows.Forms.RightToLeft.YesMe.CheckBox1.Size = New System.Drawing.Size(142, 15)Me.CheckBox1.TabIndex = 21Me.CheckBox1.Text = "Windows开机即运行"Me.CheckBox1.TextAlign = System.Drawing.ContentAlignment.MiddleRight''btnSave'Me.btnSave.BackColor = System.Drawing.SystemColors.ControlMe.btnSave.Font = New System.Drawing.Font("宋体", 10.0!)Me.btnSave.ForeColor = System.Drawing.SystemColors.ControlTextMe.btnSave.ImeMode = System.Windows.Forms.ImeMode.NoControlMe.btnSave.Location = New System.Drawing.Point(0, 16)Me.btnSave.Name = "btnSave"Me.btnSave.Size = New System.Drawing.Size(144, 24)Me.btnSave.TabIndex = 32Me.btnSave.Text = "保存设置"''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.FixedToolWindowMe.Name = "Form1"Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreenMe.Text = "Windows开机即运行"Me.ResumeLayout(False)End Sub#End RegionDim Reg As Microsoft.Win32.RegistryKeyPrivate Sub InitVar()If btnSave.Visible = True ThenEnd IfReg = CurrentUser.OpenSubKey("Software/Microsoft/Windows/CurrentVersion/Run", True)If Reg.GetValue("MengXianHui") <> "" ThenCheckBox1.Checked = TrueElseCheckBox1.Checked = FalseEnd IfEnd SubPrivate Sub SaveSettings()If CheckBox1.Checked = True ThenReg = CurrentUser.OpenSubKey("Software/Microsoft/Windows/CurrentVersion/Run", True)Reg.SetValue("MengXianHui", Application.ExecutablePath)ElseReg = CurrentUser.OpenSubKey("Software/Microsoft/Windows/CurrentVersion/Run", True)Reg.SetValue("MengXianHui", "")End IfInitVar()MessageBox.Show("您已经设置了,请重新启动计算机看效果。", "提示", _MessageBoxButtons.OK, MessageBoxIcon.Information)If CheckBox1.Checked = True ThenMe.Dispose(True)End IfEnd SubPrivate Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _Handles btnSave.ClickSaveSettings()End SubEnd Class