从 VBScript 创建快捷方式几乎可以正常工作
Create shortcut from VBScript almost works fine
我创建了一个创建快捷方式的 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();
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
属性.
在 link 文件中处理的
此外,虽然这次不需要,但使用 FileSystemObject
的 BuildPath
方法连接路径以避免可能出现的问题(如前所述,在本例中不是,但 . ..) 直接连接字符串时使用双反斜杠。
我创建了一个创建快捷方式的 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();
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
的 BuildPath
方法连接路径以避免可能出现的问题(如前所述,在本例中不是,但 . ..) 直接连接字符串时使用双反斜杠。