问题
I get an error that on line 6, the one with lnk.TargetPath, that the argument is invalid. I am hoping to make a link to this program under the start menu on the desktop. Anybody know why it is doing this?
Set objShell = WScript.CreateObject("WScript.Shell")
Set lnk = objShell.CreateShortcut("C:\Users\%USERDATA%\Desktop\Shutdown.LNK")
Dim strUserProfile
strUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
lnk.TargetPath = "C:\Users\" & strUserProfile & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\shutdown.bat"
lnk.Arguments = ""
lnk.Description = "Shutdown"
'lnk.HotKey = "ALT+CTRL+F"
lnk.IconLocation = "C:\Users\" & strUserProfile & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\shutdown.bat, 2"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = "C:\Users\" & strUserProfile &"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
lnk.Save
Set lnk = Nothing
回答1:
I think it's because strUserProfiles holds the full path of user directory. Try this slightly modified code:
Set objShell = WScript.CreateObject("WScript.Shell")
Dim strUserProfile
strUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")
Set lnk = objShell.CreateShortcut(strUserProfile & "\Desktop\Shutdown.LNK")
lnk.TargetPath = strUserProfile & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\shutdown.bat"
lnk.Arguments = ""
lnk.Description = "Shutdown"
'lnk.HotKey = "ALT+CTRL+F"
lnk.IconLocation = strUserProfile & "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\shutdown.bat, 2"
lnk.WindowStyle = "1"
lnk.WorkingDirectory = strUserProfile &"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs"
lnk.Save
Set lnk = Nothing
来源:https://stackoverflow.com/questions/26006735/creating-a-lnk-in-vbs