以下的StartupPath是开机自启的路径【默认为添加自身到开机自启】,也可自行修改指定目标为”C:\1.exe”或者”D:\2.ahk”
示例:
PowerBoot(“开机启动自身的VBS名称”, 3) ; 延时3秒后启动此ahk
; 添加开机自启动,延时功能以秒为单位【vbs带if判断,即使目标不存在也不会报错】
PowerBoot(VBSFileName:="StartupVBS", Sleep:=0, StartupPath:="") {
FileDelete, %A_StartMenu%\Programs\Startup\%VBSFileName%.vbs
StartupPath := StartupPath="" ? A_ScriptFullPath : StartupPath
FileAppend, Set shell=CreateObject("Wscript.Shell")`nSet fs=CreateObject("Scripting.FileSystemObject")`nif fs.FileExists("%StartupPath%") then`nWscript.Sleep 1000*%Sleep%`nshell.Run"""%StartupPath%"""`nend if, %A_StartMenu%\Programs\Startup\%VBSFileName%.vbs
Return ErrorLevel
}
; 删除自启动
DeletePowerBoot(VBSFileName:="StartupVBS") {
FileDelete, %A_StartMenu%\Programs\Startup\%VBSFileName%.vbs
}
; 检查开机自启动脚本路径是否正确匹配【正确返回0,错误返回1】
ExaminePowerBoot(VBSFileName:="StartupVBS", StartupPath:="") {
FileRead, VBSContent, %A_StartMenu%\Programs\Startup\%VBSFileName%.vbs
StartupPath := StartupPath="" ? A_ScriptFullPath : StartupPath
Return InStr(VBSContent, StartupPath)=0 ? 1 : 0
}
检查开机自启项是否存在或正确的方法
if ExaminePowerBoot("演示开机自启") {
MsgBox 0x40034, 开机自启已失效, 脚本路径改变,导致开机自启失效`n是否恢复开机自启功能?
IfMsgBox Yes, {
FileDelete, %A_StartMenu%\Programs\Startup\演示开机自启.vbs
MsgBox % PowerBoot("演示开机自启")
}
}
还用vbs实现的?
嗯,我的情况将AHK放在自启文件夹启动不成功。VBS系统自带的,用VBS启动AHK没问题