Windows 脚本宿主  

CreateShortcut 方法

创建新的快捷方式,或打开现有的快捷方式。

object.CreateShortcut(strPathname) 

参数

object
WshShell 对象。
strPathname
表示要创建的快捷方式的路径名的字符串值。

说明

CreateShortcut 方法返回 WshShortcut 对象或 WshURLShortcut 对象。只调用 CreateShortcut 方法并不会导致快捷方式的创建。快捷方式对象以及对其所做的更改存储在内存中,直到您用 Save 方法将其保存到磁盘中为止。要创建快捷方式,必须执行以下操作:

  1. 创建 WshShortcut 对象的实例。
  2. 初始化它的属性。
  3. 用 Save 方法将其保存到磁盘中。
    注意   一个常见的问题是,将参数放在快捷方式对象的 TargetPath 属性中将不起作用。快捷方式的所有参数都必须放在 Arguments 属性中。

示例

下面的示例创建 WshShell 对象并使用 CreateShortcut 方法创建两个快捷方式。

<package>
   <job id="vbs">
      <script language="VBScript">
         set WshShell = WScript.CreateObject("WScript.Shell")
         strDesktop = WshShell.SpecialFolders("Desktop")
         set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
         oShellLink.TargetPath = WScript.ScriptFullName
         oShellLink.WindowStyle = 1
         oShellLink.Hotkey = "CTRL+SHIFT+F"
         oShellLink.IconLocation = "notepad.exe, 0"
         oShellLink.Description = "Shortcut Script"
         oShellLink.WorkingDirectory = strDesktop
         oShellLink.Save
         set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
         oUrlLink.TargetPath = "http://www.microsoft.com/china"
         oUrlLink.Save
      </script>
   </job>

   <job id="js">
      <script language="JScript">
         var WshShell = WScript.CreateObject("WScript.Shell");
         strDesktop = WshShell.SpecialFolders("Desktop");
         var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
         oShellLink.TargetPath = WScript.ScriptFullName;
         oShellLink.WindowStyle = 1;
         oShellLink.Hotkey = "CTRL+SHIFT+F";
         oShellLink.IconLocation = "notepad.exe, 0";
         oShellLink.Description = "Shortcut Script";
         oShellLink.WorkingDirectory = strDesktop;
         oShellLink.Save();
         var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
         oUrlLink.TargetPath = "http://www.microsoft.com/china";
         oUrlLink.Save();
      </script>
   </job>
</package>

请参阅

运行脚本 | WshShortcut 对象 | WshUrlShortcut 对象 | WshShell 对象