2014-09-24 26 views
1

我得到一個錯誤,在第6行,有一個lnk.TargetPath,該參數是無效的。我希望在桌面上的開始菜單下建立一個到這個程序的鏈接。有人知道它爲什麼這樣做?在VBS中創建一個lnk

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 

回答

2

我想這是因爲strUserProfiles包含用戶目錄的完整路徑。試試這個稍作修改的代碼:

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 
+0

謝謝。我將在pastebin上發佈我的當前代碼。 – 2014-09-24 23:30:56

+0

[鏈接](http://pastebin.com/vgEQ0JAc) – 2014-09-24 23:32:29

+0

這就是我在pastebin上完成的代碼。 – 2014-09-24 23:32:49