2013-10-14 155 views
0

我正在使用VBscript來修改快捷方式的目標路徑,但我無法做到這一點。當我運行腳本時,我沒有得到任何錯誤,但是好像該腳本沒有做任何事情,那就是目標路徑沒有改變。這是我的腳本無法修改快捷方式路徑

Private Sub Change_ShortCut(TargetPath, ShortCutPath, ShortCutname, WorkPath, ServerName, PortNumber, ServiceName, ExecVar) 
Dim VbsObj 
Dim userName 
Dim hostName 

' Get user name and host name from another method 
Get_UserName userName, hostName 

Const ALL_USERS_DESKTOP = &H19& 
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.Namespace(ALL_USERS_DESKTOP) 
Set objFolderItem = objFolder.ParseName("App1.lnk") 
Set objShellLink = objFolderItem.GetLink 
Wscript.Echo objShellLink.Path 
objShellLink.Path = TargetPath & ServerName & " " & PortNumber & " " & ServiceName & " " & userName & " " & hostName & " 20 -exec=" & """" & "\""" & ExecVar & "\""" &"""" 
objShellLink.Save 

End Sub 

回答

2

你在努力嘗試。使用WshShell對象,而不是:

Set fso = CreateObject("Scripting.FileSystemObject") 
Set sh = CreateObject("WScript.Shell") 

lnkpath = fso.BuildPath(sh.SpecialFolders("AllUsersDesktop"), "App1.lnk") 
Set lnk = sh.CreateShortcut(lnkpath) 
lnk.TargetPath = TargetPath & ServerName & " " & PortNumber & " " & ServiceName _ 
    & " " & userName & " " & hostName & " 20 -exec=""\""" & ExecVar & "\""""" 
lnk.Save 

如果你有一個域環境中,使用Group Policy Preferences可能是更新的快捷方式更好的方法。