东海县临终服务网

使用VBS创建快捷方式的代码

2026-03-29 18:33:02 浏览次数:0
详细信息

方法1:创建桌面快捷方式(指向可执行文件)

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")

Set oShellLink = WshShell.CreateShortcut(strDesktop & "\我的程序.lnk")
oShellLink.TargetPath = "C:\Program Files\MyApp\myapp.exe"
oShellLink.WorkingDirectory = "C:\Program Files\MyApp"
oShellLink.Description = "我的应用程序"
oShellLink.IconLocation = "C:\Program Files\MyApp\myapp.exe,0"
oShellLink.WindowStyle = 1
oShellLink.Save

方法2:创建开始菜单快捷方式

Set WshShell = WScript.CreateObject("WScript.Shell")
strStartMenu = WshShell.SpecialFolders("StartMenu")

Set oShellLink = WshShell.CreateShortcut(strStartMenu & "\我的程序.lnk")
oShellLink.TargetPath = "C:\Windows\System32\notepad.exe"
oShellLink.Arguments = "C:\myfile.txt"
oShellLink.WorkingDirectory = "C:\"
oShellLink.Hotkey = "CTRL+ALT+N"
oShellLink.Save

方法3:创建URL快捷方式

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")

Set oUrlLink = WshShell.CreateShortcut(strDesktop & "\百度.lnk")
oUrlLink.TargetPath = "https://www.baidu.com"
oUrlLink.Save

方法4:创建带参数的快捷方式

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")

Set oShellLink = WshShell.CreateShortcut(strDesktop & "\CMD管理员.lnk")
oShellLink.TargetPath = "C:\Windows\System32\cmd.exe"
oShellLink.Arguments = "/k echo 以管理员身份运行"
oShellLink.WorkingDirectory = "%HOMEDRIVE%%HOMEPATH%"
oShellLink.IconLocation = "C:\Windows\System32\cmd.exe,0"
' 如果需要以管理员身份运行,需要额外设置(VBS本身无法直接设置)
oShellLink.Save

方法5:创建快捷方式到指定文件夹

Set WshShell = WScript.CreateObject("WScript.Shell")

' 创建到指定位置的快捷方式
Set oShellLink = WshShell.CreateShortcut("C:\Shortcuts\计算器.lnk")
oShellLink.TargetPath = "C:\Windows\System32\calc.exe"
oShellLink.Description = "Windows计算器"
oShellLink.IconLocation = "C:\Windows\System32\calc.exe,0"
oShellLink.Save

完整示例:带错误处理的创建快捷方式

Option Explicit

Dim WshShell, strDesktop, oShellLink

On Error Resume Next

Set WshShell = WScript.CreateObject("WScript.Shell")
If Err.Number <> 0 Then
    WScript.Echo "错误: 无法创建WScript.Shell对象"
    WScript.Quit(1)
End If

strDesktop = WshShell.SpecialFolders("Desktop")
If Err.Number <> 0 Then
    WScript.Echo "错误: 无法获取桌面路径"
    WScript.Quit(1)
End If

Set oShellLink = WshShell.CreateShortcut(strDesktop & "\记事本.lnk")
If Err.Number <> 0 Then
    WScript.Echo "错误: 无法创建快捷方式对象"
    WScript.Quit(1)
End If

With oShellLink
    .TargetPath = "C:\Windows\System32\notepad.exe"
    .WorkingDirectory = "%USERPROFILE%\Documents"
    .Description = "Windows记事本"
    .IconLocation = "C:\Windows\System32\notepad.exe,0"
    .WindowStyle = 1  ' 1=正常窗口,3=最大化,7=最小化
    .Hotkey = ""      ' 快捷键,如"CTRL+ALT+N"
    .Save
End With

If Err.Number = 0 Then
    WScript.Echo "快捷方式创建成功!"
Else
    WScript.Echo "错误: " & Err.Description
End If

特殊文件夹路径常量:

使用方法:

将代码保存为 .vbs 文件(如 create_shortcut.vbs) 双击运行VBS文件 根据需要修改目标路径、名称等参数

注意:在Windows 10/11上,某些位置可能需要管理员权限才能写入。

相关推荐