Vbscript 将脚本放入windows启动

Vbscript 将脚本放入windows启动,vbscript,startup,Vbscript,Startup,我正在尝试编写代码,将自己放在windows启动中,并使用vbs在启动时运行。我编写了一个将文件复制到启动文件夹的程序,但在系统启动时不执行 代码如下: Set objShell = Wscript.CreateObject("Wscript.Shell") strPath = objShell.SpecialFolders("Startup") strMyPath = strPath&"\" Const SourceFile = "a.vbs" strMyPath = strMyP

我正在尝试编写代码,将自己放在windows启动中,并使用vbs在启动时运行。我编写了一个将文件复制到启动文件夹的程序,但在系统启动时不执行

代码如下:

Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Startup")
strMyPath = strPath&"\"  
Const SourceFile = "a.vbs"
strMyPath = strMyPath & SourceFile
Set fso = CreateObject("Scripting.FileSystemObject")

'Check to see if the file already exists in the destination folder
If fso.FileExists(strMyPath) Then

'Check to see if the file is read-only
If Not fso.GetFile(strMyPath).Attributes And 1 Then 
    'The file exists and is not read-only.  Safe to replace the file.
    fso.CopyFile SourceFile, strMyPath, True
Else 
    'The file exists and is read-only.
    'Remove the read-only attribute
    fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes - 1
    'Replace the file
    fso.CopyFile SourceFile, strMyPath, True
    'Reapply the read-only attribute
    fso.GetFile(strMyPath).Attributes = fso.GetFile(strMyPath).Attributes + 1
End If
Else
'The file does not exist in the destination folder.  Safe to copy file to this folder.
fso.CopyFile SourceFile, strMyPath, True
End If
Set fso = Nothing

可以执行此脚本将vbs文件插入HKLM\startup path或HKCU\startup path。区别在于HKLM适用于所有用户,而HKCU仅适用于激活此脚本的当前用户

Const cHKLM = &H80000002
Const cComp = "."
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
cComp & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
strValueName = "ScriptName"
strValue = "Drive:\PathTo\Myscript.vbs"
objReg.SetStringValue cHKLM, strKeyPath, strValueName, strValue
祝你好运