首先声明API Private Declare Function fCreateShellLink Lib "vb5stkit.dll" (ByVal lpStrFolderName As String, ByVal lpStrLinkName As String, ByVal lpStrLinkPath As String, ByVal lpStrLinkArgs As String) As Long之后在Command1的Click事件中 fCreateShellLink "c:\www", "快捷方式", "d:\wwf\cdps.exe", ""
问题: 如何获取已存在的快捷方式(*.lnk)的所有信息?及如何在 Windows 任务栏中创建快捷方式? 解答: 引用 Windows Script Host Model Dim x As New IWshRuntimeLibrary.IWshShell_Class Dim y As IWshRuntimeLibrary.IWshShortcut_Class Set y = x.CreateShortcut("..\..\XXX.lnk") Dim s As String s = "Arguments: " & y.Arguments & vbCrLf _ & "Description: " & y.Description & vbCrLf _ & "FullName: " & y.FullName & vbCrLf _ & "Hotkey: " & y.Hotkey & vbCrLf _ & "IconLocation: " & y.IconLocation & vbCrLf _ & "TargetPath: " & y.TargetPath & vbCrLf _ & "WindowStyle: " & y.WindowStyle & vbCrLf _ & "WorkingDirectory: " & y.WorkingDirectory VBA.MsgBox s '=============== Dim x As New IWshRuntimeLibrary.IWshShell_Class Dim y As IWshRuntimeLibrary.IWshShortcut_Class If VBA.Len(VBA.Trim(VBA.Dir(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\"))) > 0 Then Set y = x.CreateShortcut(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\WinRAR.lnk") y.TargetPath = "..\..\XXX.exe" y.Save '创建快捷 End If '下面是几种 Windows 特殊路径: AllUsersDesktop AllUsersStartMenu AllUsersPrograms AllUsersStartup AppData Desktop Favorites Fonts MyDocuments NetHood PrintHood Programs Recent SendTo StartMenu Startup Templates 综上 CreateShortcut 是用来创建一个 WshShortcut 的对象, 只要不调用其 Save 方法,就不会真正改变快捷方式的属性。 另外 Windows Script Host Model 的功能还有很多的,例如: 注册表读写、NetWork 等!
'创建快捷方式 Sub CreateShortCuts() Dim WSH_Shell As WshShell Dim SCut As WshShortcut
Set WSH_Shell = New WshShell
Set SCut = WSH_Shell.CreateShortcut(TOUCHPATH & "\touch.lnk")
With SCut .TargetPath = TOUCHPATH & "\touch.exe" .WorkingDirectory = TOUCHPATH .Save End With
Set WSH_Shell = Nothing Set SCut = Nothing
Dim FileNumber As Integer
FileNumber = FreeFile
'pif快捷方式的第430字节为8,全屏幕运行 Open TOUCHPATH & "\touch.pif" For Binary As #FileNumber Put #FileNumber, 430, 8 Close #FileNumber
Private Declare Function fCreateShellLink Lib "vb5stkit.dll" (ByVal lpStrFolderName As String, ByVal lpStrLinkName As String, ByVal lpStrLinkPath As String, ByVal lpStrLinkArgs As String) As Long之后在Command1的Click事件中
fCreateShellLink "c:\www", "快捷方式", "d:\wwf\cdps.exe", ""
我用的是VB6.0
解答: 引用 Windows Script Host Model
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim y As IWshRuntimeLibrary.IWshShortcut_Class
Set y = x.CreateShortcut("..\..\XXX.lnk")
Dim s As String
s = "Arguments: " & y.Arguments & vbCrLf _
& "Description: " & y.Description & vbCrLf _
& "FullName: " & y.FullName & vbCrLf _
& "Hotkey: " & y.Hotkey & vbCrLf _
& "IconLocation: " & y.IconLocation & vbCrLf _
& "TargetPath: " & y.TargetPath & vbCrLf _
& "WindowStyle: " & y.WindowStyle & vbCrLf _
& "WorkingDirectory: " & y.WorkingDirectory
VBA.MsgBox s
'===============
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim y As IWshRuntimeLibrary.IWshShortcut_Class
If VBA.Len(VBA.Trim(VBA.Dir(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\"))) > 0 Then
Set y = x.CreateShortcut(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\WinRAR.lnk")
y.TargetPath = "..\..\XXX.exe"
y.Save '创建快捷
End If
'下面是几种 Windows 特殊路径:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
AppData
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
综上 CreateShortcut 是用来创建一个 WshShortcut 的对象, 只要不调用其 Save 方法,就不会真正改变快捷方式的属性。
另外 Windows Script Host Model 的功能还有很多的,例如: 注册表读写、NetWork 等!
Sub CreateShortCuts()
Dim WSH_Shell As WshShell
Dim SCut As WshShortcut
Set WSH_Shell = New WshShell
Set SCut = WSH_Shell.CreateShortcut(TOUCHPATH & "\touch.lnk")
With SCut
.TargetPath = TOUCHPATH & "\touch.exe"
.WorkingDirectory = TOUCHPATH
.Save
End With
Set WSH_Shell = Nothing
Set SCut = Nothing
Dim FileNumber As Integer
FileNumber = FreeFile
'pif快捷方式的第430字节为8,全屏幕运行
Open TOUCHPATH & "\touch.pif" For Binary As #FileNumber
Put #FileNumber, 430, 8
Close #FileNumber
End Sub'引用Windows Script Host Model