VB.net如何编写随Windows启动运行的程序
通过设置和读取注册表,可以实现我们的应用程序随Windows的启动即可执行的功能。下面就是实现的代码: &{:2{_
gs\>&?CM
=U[hcA
(DQ96|Pco
Form1.vb k2 Yb9
/%c`Qb
Imports Microsoft.Win32.Registry UP@Si* v
Public Class Form1 &1a#5J="v]
Inherits System.Windows.Forms.Form NaeQ2iMX
#Region " Windows 窗体设计器生成的代码 " ,[ k
Public Sub New() rWNXux
MyBase.New() c$;">/
'该调用是 Windows 窗体设计器所必需的。 joZ Hd`
InitializeComponent() 0?;to.fW
'在 InitializeComponent() 调用之后添加任何初始化 9sD+
End Sub 8Q>e$[\nm
+P@4<0a_o
'窗体重写处置以清理组件列表。 <ci[ pP
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 5d &J% a
If disposing Then J= #w!
If Not (components Is Nothing) Then @,@\Xby
components.Dispose() Q5<|R=2
End If Ef19O[#
End If JuBL>k4M
MyBase.Dispose(disposing) ;K p#0'
End Sub $#~^KYhiB
Z,gTZ59.Db
'Windows 窗体设计器所必需的 7Yha9 d4
Private components As System.ComponentModel.IContainer eS-L-\v
e~KC@Z>
'注意:以下过程是 Windows 窗体设计器所必需的 4FeSQ-\
'可以使用 Windows 窗体设计器修改此过程。 \DZ_'7N
'不要使用代码编辑器修改它。 `]]nV<&
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox C`nQjP
Friend WithEvents btnSave As System.Windows.Forms.Button bQFH !1U
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() T]1z$-S
Me.CheckBox1 = New System.Windows.Forms.CheckBox() t2g+# {h
Me.btnSave = New System.Windows.Forms.Button() ld*&ddwJ
Me.SuspendLayout() UQWR>n
' h<&Z*g?#UO
'CheckBox1 (ZKmPd [
' q^CL$QXD6y
Me.CheckBox1.BackColor = System.Drawing.SystemColors.Control e' R{Y-l
Me.CheckBox1.Checked = True [u(wn_# v
Me.CheckBox1.CheckState = System.Windows.Forms.CheckState.Checked S*.Ns
Me.CheckBox1.ForeColor = System.Drawing.SystemColors.ControlText I#Zk45
Me.CheckBox1.ImeMode = System.Windows.Forms.ImeMode.NoControl ^mEZp2`
Me.CheckBox1.Name = "CheckBox1" |>}b*KGOg
Me.CheckBox1.RightToLeft = System.Windows.Forms.RightToLeft.Yes ^O? ^W{
Me.CheckBox1.Size = New System.Drawing.Size(142, 15) A o/%+o]E
Me.CheckBox1.TabIndex = 21 SUg\ ;a!
Me.CheckBox1.Text = "Windows开机即运行" >,,A<g8
Me.CheckBox1.TextAlign = System.Drawing.ContentAlignment.MiddleRight T:^QT.?_i
' G& |K
'btnSave KO~+cf>K]
' 8"yI/PSY
Me.btnSave.BackColor = System.Drawing.SystemColors.Control =HFpF^3&`
Me.btnSave.Font = New System.Drawing.Font("宋体", 10.0!) gnY521E
Me.btnSave.ForeColor = System.Drawing.SystemColors.ControlText K*A~QqQB9V
Me.btnSave.ImeMode = System.Windows.Forms.ImeMode.NoControl 1`"~!Eot
Me.btnSave.Location = New System.Drawing.Point(0, 16) {\wEo#`K
Me.btnSave.Name = "btnSave" UFn^MP `
Me.btnSave.Size = New System.Drawing.Size(144, 24) q? yr]
Me.btnSave.TabIndex = 32 mdc6c,
Me.btnSave.Text = "保存设置" Y-Q s[N.F&
' >>ZRb5`Z5o
'Form1 k>H1i
' r\h( [G%T
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Y]Eo$G;u
Me.ClientSize = New System.Drawing.Size(144, 40) k8zI8% w
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSave, Me.CheckBox1}) F-4;:j`A`J
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow tUAX]N
Me.Name = "Form1" H9feiw2G
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen &>;y6(xQ0l
Me.Text = "Windows开机即运行" MO1:H?D
Me.ResumeLayout(False) p1p!VL
A+t! 1tm
End Sub - ];ub6s/
J">R "b
#End Region bWH"V:rn
2fs)4Yr{L
Dim Reg As Microsoft.Win32.RegistryKey p`= -miL
Private Sub InitVar() m&9C@ +
If btnSave.Visible = True Then i.rxOww|_r
tim,wiT~
End If TZ{sQPY
Reg = CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True) Ntv\8 4~|
If Reg.GetValue("MengXianHui") <> "" Then _TsID%y bd
CheckBox1.Checked = True ?GxbA4-K
Else HGH *\W&
CheckBox1.Checked = False onQpa
End If e{ Pp8l%S
End Sub v/RA+4r=
Private Sub SaveSettings() yz>r#f
If CheckBox1.Checked = True Then P.FV=A
Reg = CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True) H6y]y'nXDY
Reg.SetValue("MengXianHui", Application.ExecutablePath) &'U..Y f
Else 6|[P,iy
Reg = CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True) lOE3>DKl
Reg.SetValue("MengXianHui", "") RdV.d7v
End If \ }1Yy#&
InitVar() pW~K")K6n
MessageBox.Show("您已经设置了,请重新启动计算机看效果。", "提示", _ 4.8W "ET+
MessageBoxButtons.OK, MessageBoxIcon.Information) c]W=i$yi
If CheckBox1.Checked = True Then 33VO4UA+
Me.Dispose(True) 6pO W(
End If +{qs^ H
End Sub Rn}BU}cQ
e"8V:P/]dL
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Szf[#@a
Handles btnSave.Click G@#>Lx>P
SaveSettings() ut O+kFox
End Sub ^.ix9q:$"
End Class Y7J3 h
ex%F:
查看本文来源