命令行创建快捷方式

什么是快捷方式?

快捷方式是一种超链接,它能定位到指定的资源并打开它,无论是在app中还是桌面应用开发中都扮演着重要的角色
我们都知道可以通过windows图形界面创建我们的快捷方式,那么你知道如何用命令行创建应用快捷方式吗?

什么是wscript?

wscript是windows自己的内置脚本,它可以完成windows一些指令。比如:创建桌面快捷方式等。

利用wsript创建桌面快捷方式

我们以创建QQ快捷方式为例
假设QQ.exe文件的路径是D:\QQ\QQ.exe
我们新建一个.txt文件(记事本)
然后写入wscript脚本

1.用绝对路径的方法

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "/腾讯QQ.lnk")
oShellLink.TargetPath = "D:\QQ\QQ.exe"
oShellLink.WindowStyle = 3
oShellLink.Hotkey = "Ctrl+Alt+e"
oShellLink.IconLocation = "D:\QQ\QQ.exe,0"
oShellLink.Description = "快捷方式"
oShellLink.WorkingDirectory = "D:\QQ"
oShellLink.Save

写好后保存为.vbs文件
由于wscrirt不能写代码注释,所以下面的代码,只是解释下脚本中每一行代码的意思,但是它不能直接运行,要运行需要复制上面的脚本

1.解释绝对路径方法(不能运行)

//创建对象实例
Set WshShell = WScript.CreateObject("WScript.Shell")

//在桌面创建快捷方式
strDesktop = WshShell.SpecialFolders("Desktop")

//创建快捷方式实例  腾讯QQ.lnk是快捷方式  文件名
set oShellLink = WshShell.CreateShortcut(strDesktop & "/腾讯QQ.lnk")

//exe程序的源路径
oShellLink.TargetPath = "D:\QQ\QQ.exe"

//默认风格3
oShellLink.WindowStyle = 3

//绑定触发快捷方式的快捷键,按下后快捷方式会打开
oShellLink.Hotkey = "Ctrl+Alt+e"

//图标资源路径,0代表列表中第一张图片(图片格式严格ico格式)
//怎么png,jpg->ico(文章结尾给你个好网站,可以在线操作)
//.ico文件被集成到.exe文件中(所以这里图片路径为.exe)
oShellLink.IconLocation = "D:\QQ\QQ.exe,0"

//快捷方式索引,哈希值相当于key
oShellLink.Description = "快捷方式"

//exe程序上一级目录,就是工作路径
oShellLink.WorkingDirectory = "D:\QQ"

//保存并创建快捷方式
oShellLink.Save

2.用相对路径办法

我们再举一个例子,看下面这个文件夹

我想在这个目录下写一个脚本,然后自动获取当前盘路径。这样我们的脚本就能在任何一台电脑下运行了。
这里icon文件夹放的是图标文件(.ico格式)

wscript代码(保存.vbs文件 可运行):

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "/鲜花管理系统.lnk")
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
StrCurPath = fso.GetFolder(".")
oShellLink.TargetPath = StrCurPath+"\鲜花销售系统.exe"
oShellLink.WindowStyle = 3
oShellLink.Hotkey = "Ctrl+Alt+e"
oShellLink.IconLocation = StrCurPath+"\icon\swing.ico, 0"
oShellLink.Description = "快捷方式"
oShellLink.WorkingDirectory = StrCurPath
oShellLink.Save

下面是代码解读(wscript不支持注释,不能运行)
只是解释含义,运行要复制上面代

//创建对象实例
Set WshShell = WScript.CreateObject("WScript.Shell")

//指定在桌面创建快捷方式(也可以直接str='其他路径')
strDesktop = WshShell.SpecialFolders("Desktop")

//在指定路径下创建快捷方式
set oShellLink = WshShell.CreateShortcut(strDesktop & "/鲜花管理系统.lnk")

//声明Dim(Dimo---小样)
Dim fso

//创建对象实例
Set fso = CreateObject("Scripting.FileSystemObject")

//获取当前资源路径(注意是vbs文件【.vbs文件】的上一层目录)
//假如exe在D:\nihao\qq.exe
//获取到的是D:\nihao
StrCurPath = fso.GetFolder(".")

//exe文件路径
oShellLink.TargetPath = StrCurPath+"\鲜花销售系统.exe"

//默认风格
oShellLink.WindowStyle = 3

//绑定快捷方式启动的快捷键
oShellLink.Hotkey = "Ctrl+Alt+e"

//图标路径
oShellLink.IconLocation = StrCurPath+"\icon\swing.ico, 0"

//哈希值,索引
oShellLink.Description = "快捷方式"

//工作目录 exe所在文件夹目录
oShellLink.WorkingDirectory = StrCurPath

//保存并执行
oShellLink.Save

格式转换

png,jpg如何转.ico?,有网站 转换ICO格式
记住要转换尺寸大一点,不然会模糊

效果展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值