我创建了一个创建快捷方式的VBScript。快捷方式可以正常工作,但是如果我将快捷方式移至其他位置,则应用会崩溃,即快捷方式失败。
如果我手动创建快捷方式,则可以将其放置在任何地方,所以我猜我的代码缺少某些内容
这是.VBS
脚本:
Set oWS = WScript.CreateObject("WScript.Shell")
relativePath = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oLink = oWS.CreateShortcut(relativePath&"\MyApp\bin\Debug\MyApp.lnk")
oLink.TargetPath = relativePath&"\MyApp\bin\Debug\MyApp.exe"
oLink.Save
如果我从C#代码创建快捷方式,也会发生同样的事情:
WshShell shell = new WshShell();
string shortcutAddress = @"C:\Users\me\Desktop\AppsSolutionsMyApp\bin\Debug\shortcut.lnk";
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
shortcut.Description = "New shortcut for my app";
shortcut.Hotkey = "Ctrl+Shift+N";
shortcut.TargetPath = @"C:\Users\me\Desktop\AppsSolutionsMyApp\bin\Debug\MyApp.exe";
shortcut.Save();
答案 0 :(得分:2)
Option Explicit
Dim baseFolder, linkFile, targetPath
With WScript.CreateObject("Scripting.FileSystemObject")
baseFolder = .BuildPath( .GetParentFolderName( WScript.ScriptFullName ), "MyApp\Bin\Debug" )
linkFile = .BuildPath( baseFolder, "MyApp.lnk" )
targetPath = .BuildPath( baseFolder, "MyApp.exe" )
End With
With WScript.CreateObject("WScript.Shell").CreateShortcut( linkFile )
.TargetPath = targetPath
.WorkingDirectory = baseFolder
.Save
End With
会影响流程启动行为的一件事是新流程的默认目录将是。这是由WorkingDirectory
属性在链接文件中处理的。
此外,虽然这次不需要,但使用FileSystemObject
的{{1}}方法连接路径可以避免可能的问题(如上所述,不是在这种情况下,而是... )在直接连接字符串时使用双反斜杠。