I try to execute this in a file called 'addcurrentkey.vbs'
But it says ')' is expected in row 1. Character 38.
I tried this tutorial: http://www.codeproject.com/Articles/16569/Autorun-Applications
Why can't I execute a .vbs file?
Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.SetValue(name, path)
End Sub
The code you posted is probably written in VB.net (or perhaps VBA). You are tying to run the code as VBScript, which does not support typed parameters and variables. It also doesn't provide the registry object you're trying to use. Change the procedure from this:
Private Sub AddCurrentKey(ByVal name As String, ByVal path As String)
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
key.SetValue(name, path)
End Sub
to this:
Private Sub AddCurrentKey(ByVal name, ByVal path)
Dim key : key = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
CreateObject("WScript.Shell").RegWrite key & "\" & name, path
End Sub
and the problem will disappear.
VBScript still uses the "old" Visual Basic syntax. Which distinguishes between function calls used in expressions and procedure calls that are statements. You use (parentheses) in an expression but not in a statement. Fix:
key.SetValue name, path
Or if you prefer:
Call key.SetValue(name, path)
The error message is hard to interpret because the script interpreter thinks that you are trying to write this:
key.SetValue (name), path
Which means something completely different. VBScript passes arguments ByRef. The extra parentheses around name turns it into an expression that creates a copy of the variable. It can be modified by the SetValue procedure without it affecting the name variable. Not what it actually does nor what you intended.