读书人

如何判断程序是否已经设置了开机启动

发布时间: 2012-03-27 13:44:24 作者: rapoo

怎么判断程序是否已经设置了开机启动?
我设置了一个程序,里面有两个按钮,实现了开机启动,下面这两段代码。

VB code
'开机启动Private Sub command1_Click()Dim wSet w = CreateObject("wscript.shell")w.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"MsgBox "设置自启动成功!"End Sub'取消开机启动Private Sub command2_Click()Dim wSet w = CreateObject("wscript.shell")w.regdelete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXENameMsgBox "取消自启动成功!"End Sub


现在我想对这个进行一些修改,我看很多程序,他们都用一个check1来实现,我搜不到这样的源代码所以也不知道他们怎么实现的,我想了想大概是如下内容,代码如图所示:



我想知道的就是,这里面我用汉字代替的“自启动已经存在”和“自启动不存在”这两种情况是怎么判断的?

解释如下:
1、画一个check1控件,启动程序的时候自动查看启动是否存在,如果存在,则value值为1,即check1被选中。反之不选中。
2、没有设置按钮来激发时间,而用的unload me,当退出的时候,则设置注册表:如果注册表里面没有而用户选中了则添加;如果注册表里面有而用户没选中则删除。

这只是我想的,也许不是这么实现的。所以如果能有其他的解决办法实现同样的效果也可以。

[解决办法]
'好了,这样排列一下好看多了

Option Explicit

Private Sub check1_click()
Dim w
Set w = CreateObject("wscript.shell")
Select Case Check1.Value
Case 0
w.regdelete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName
Check1.Caption = "自启动不存在"
MsgBox "取消自启动成功"
Case 1
w.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"
Check1.Caption = "自启动已经存在"
MsgBox "设置自启动成功!!"
End Select
End Sub



Private Sub Form_Load()
On Error Resume Next
Dim w
Set w = CreateObject("wscript.shell")
If w.regread("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName) = "" Then
Check1.Value = 0
Check1.Caption = "自启动不存在"
Else
Check1.Value = 1
Check1.Caption = "自启动已经存在"
End If
End Sub
[解决办法]
简单啊,一个函数搞定
VB code
Option ExplicitPrivate WshShellPrivate Sub Form_Load()    Set WshShell = CreateObject("wscript.shell")    If funKeyExist Then        Check1.Value = 1    Else        Check1.Value = 0    End IfEnd SubPrivate Sub Form_Unload(Cancel As Integer)    If Check1.Value = 1 And Not funKeyExist Then        WshShell.regwrite "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName, App.Path & "\" & App.EXEName & ".exe"        MsgBox "设置自启动成功!"    End If    If Check1.Value = 0 And funKeyExist Then        WshShell.regdelete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName        MsgBox "取消自启动成功!"    End IfEnd SubPrivate Function funKeyExist() As Boolean '//存在返回true,否则返回false    On Error Resume Next    WshShell.regread "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\" & App.EXEName '//如果不存在读取会有错    If Err Then        funKeyExist = False    Else        funKeyExist = True    End IfEnd Function 

读书人网 >VB

热点推荐