一个创建快捷方式类
2007-02-08 12:24
267 查看
Author:水如烟
利用了WScript.Shell
示例:
Namespace LzmTW.uSystem.uIO
Public Class ShortcutDemo
'示例,创建当前程序的快捷方式到桌面
Public Shared Sub CreateCurrentAppShortCutOnDesktop()
Dim args(My.Application.CommandLineArgs.Count - 1) As String
My.Application.CommandLineArgs.CopyTo(args, 0)
Dim appName As String = My.Application.Info.Title
Dim mShortcut As WshShortcut
mShortcut = WshShortcut.CreateIn(Environment.SpecialFolder.Desktop, appName)
With mShortcut
.TargetPath = Application.ExecutablePath
.Arguments = String.Join(",", args)
.Description = My.Application.Info.Description
.Hotkey = Keys.Control Or Keys.Alt Or Keys.A
.WindowStyle = WshWindowStyle.WshMaximizedFocus
.WorkingDirectory = .SpecialFolder(Environment.SpecialFolder.MyDocuments)
'.SetDefaultIcon()
.Save()
.Dispose()
End With
End Sub
End Class
End Namespace
类:
Option Strict Off
Imports System.ComponentModel
Namespace LzmTW.uSystem.uIO
Public Class WshShortcut
Implements IDisposable
Private gComIWshShortcut As Object
Private gComIWshShell3 As Object
Sub New()
gComIWshShell3 = CreateObject("WScript.Shell")
End Sub
''' <summary>
''' 打开或准备创建
''' </summary>
''' <param name="PathLink">快捷方式全名</param>
''' <remarks>如要创建或修改,配置参数后需要保存</remarks>
Public Sub Create(ByVal PathLink As String)
If Not PathLink.ToLower.EndsWith(".lnk") Then
PathLink = PathLink & ".lnk"
End If
OnlyMeDispose()
gComIWshShortcut = gComIWshShell3.CreateShortcut(PathLink)
End Sub
'舍去不用
Private Sub Load(ByVal PathLink As String)
gComIWshShortcut.Load(PathLink)
End Sub
''' <summary>
''' 保存(创建或更改当前快捷方式)
''' </summary>
Public Sub Save()
gComIWshShortcut.Save()
End Sub
''' <summary>
''' 目标
''' </summary>
Public Property TargetPath() As String
Get
Return gComIWshShortcut.TargetPath
End Get
Set(ByVal value As String)
gComIWshShortcut.TargetPath = value
End Set
End Property
''' <summary>
''' 目标参数
''' </summary>
Public Property Arguments() As String
Get
Return gComIWshShortcut.Arguments
End Get
Set(ByVal value As String)
gComIWshShortcut.Arguments = value
End Set
End Property
''' <summary>
''' 备注
''' </summary>
Public Property Description() As String
Get
Return gComIWshShortcut.Description
End Get
Set(ByVal value As String)
gComIWshShortcut.Description = value
End Set
End Property
''' <summary>
''' 快捷方式全名
''' </summary>
Public ReadOnly Property FullName() As String
Get
Return gComIWshShortcut.FullName
End Get
End Property
''' <summary>
''' 快捷键
''' </summary>
Public Property Hotkey() As Keys
Get
Return KeysConverter.ConvertFromString(gComIWshShortcut.Hotkey)
End Get
Set(ByVal value As Keys)
gComIWshShortcut.Hotkey = KeysConverter.ConvertTo(value, GetType(String))
End Set
End Property
''' <summary>
''' 图标位置
''' </summary>
Public Property IconLocation() As String
Get
Return gComIWshShortcut.IconLocation
End Get
Set(ByVal value As String)
gComIWshShortcut.IconLocation = value
End Set
End Property
''' <summary>
''' 相对路径
''' </summary>
Public WriteOnly Property RelativePath() As String
Set(ByVal value As String)
gComIWshShortcut.RelativePath = value
End Set
End Property
''' <summary>
''' 运行方式
''' </summary>
Public Property WindowStyle() As WshWindowStyle
Get
Return gComIWshShortcut.WindowStyle
End Get
Set(ByVal value As WshWindowStyle)
gComIWshShortcut.WindowStyle = value
End Set
End Property
''' <summary>
''' 起始位置
''' </summary>
Public Property WorkingDirectory() As String
Get
Return gComIWshShortcut.WorkingDirectory
End Get
Set(ByVal value As String)
gComIWshShortcut.WorkingDirectory = value
End Set
End Property
Private Sub OnlyMeDispose()
If gComIWshShortcut Is Nothing Then Return
System.Runtime.InteropServices.Marshal.ReleaseComObject(gComIWshShortcut)
gComIWshShortcut = Nothing
End Sub
''' <summary>
''' 释放内存
''' </summary>
Public Sub Dispose() Implements System.IDisposable.Dispose
OnlyMeDispose()
System.Runtime.InteropServices.Marshal.ReleaseComObject(gComIWshShell3)
gComIWshShell3 = Nothing
End Sub
''以下为补充
''' <summary>
''' 默认图标位置
''' </summary>
Public Function DefaultIcon() As String
Return "%SystemRoot%system32SHELL32.dll,30"
End Function
Public Sub SetDefaultIcon()
Me.IconLocation = Me.DefaultIcon
End Sub
''' <summary>
''' 获取常用目录
''' </summary>
Public Function SpecialFolder(ByVal folder As Environment.SpecialFolder) As String
Return Environment.GetFolderPath(folder)
End Function
''' <summary>
''' 在指定目录下打开或准备创建快捷方式
''' </summary>
''' <param name="folder">目录</param>
''' <param name="name">快捷方式名称</param>
''' <remarks>如要创建或修改,配置参数后需要保存</remarks>
Public Shared Function CreateIn(ByVal folder As Environment.SpecialFolder, ByVal name As String) As WshShortcut
Dim mShortcut As New WshShortcut
Dim mPathLink As String = String.Concat(mShortcut.SpecialFolder(folder), "\", name)
mShortcut.Create(mPathLink)
Return mShortcut
End Function
Private Shared KeysConverter As New KeysConverter
End Class
End Namespace
Namespace LzmTW.uSystem.uIO
Public Enum WshWindowStyle
'WshHide = 0
WshNormalFocus = 1
'WshMinimizedFocus = 2
WshMaximizedFocus = 3
'WshNormalNoFocus = 4
'WshMinimizedNoFocus = 6
WshMinimizedFocus = 7
End Enum
End Namespace
利用了WScript.Shell
示例:
Namespace LzmTW.uSystem.uIO
Public Class ShortcutDemo
'示例,创建当前程序的快捷方式到桌面
Public Shared Sub CreateCurrentAppShortCutOnDesktop()
Dim args(My.Application.CommandLineArgs.Count - 1) As String
My.Application.CommandLineArgs.CopyTo(args, 0)
Dim appName As String = My.Application.Info.Title
Dim mShortcut As WshShortcut
mShortcut = WshShortcut.CreateIn(Environment.SpecialFolder.Desktop, appName)
With mShortcut
.TargetPath = Application.ExecutablePath
.Arguments = String.Join(",", args)
.Description = My.Application.Info.Description
.Hotkey = Keys.Control Or Keys.Alt Or Keys.A
.WindowStyle = WshWindowStyle.WshMaximizedFocus
.WorkingDirectory = .SpecialFolder(Environment.SpecialFolder.MyDocuments)
'.SetDefaultIcon()
.Save()
.Dispose()
End With
End Sub
End Class
End Namespace
类:
Option Strict Off
Imports System.ComponentModel
Namespace LzmTW.uSystem.uIO
Public Class WshShortcut
Implements IDisposable
Private gComIWshShortcut As Object
Private gComIWshShell3 As Object
Sub New()
gComIWshShell3 = CreateObject("WScript.Shell")
End Sub
''' <summary>
''' 打开或准备创建
''' </summary>
''' <param name="PathLink">快捷方式全名</param>
''' <remarks>如要创建或修改,配置参数后需要保存</remarks>
Public Sub Create(ByVal PathLink As String)
If Not PathLink.ToLower.EndsWith(".lnk") Then
PathLink = PathLink & ".lnk"
End If
OnlyMeDispose()
gComIWshShortcut = gComIWshShell3.CreateShortcut(PathLink)
End Sub
'舍去不用
Private Sub Load(ByVal PathLink As String)
gComIWshShortcut.Load(PathLink)
End Sub
''' <summary>
''' 保存(创建或更改当前快捷方式)
''' </summary>
Public Sub Save()
gComIWshShortcut.Save()
End Sub
''' <summary>
''' 目标
''' </summary>
Public Property TargetPath() As String
Get
Return gComIWshShortcut.TargetPath
End Get
Set(ByVal value As String)
gComIWshShortcut.TargetPath = value
End Set
End Property
''' <summary>
''' 目标参数
''' </summary>
Public Property Arguments() As String
Get
Return gComIWshShortcut.Arguments
End Get
Set(ByVal value As String)
gComIWshShortcut.Arguments = value
End Set
End Property
''' <summary>
''' 备注
''' </summary>
Public Property Description() As String
Get
Return gComIWshShortcut.Description
End Get
Set(ByVal value As String)
gComIWshShortcut.Description = value
End Set
End Property
''' <summary>
''' 快捷方式全名
''' </summary>
Public ReadOnly Property FullName() As String
Get
Return gComIWshShortcut.FullName
End Get
End Property
''' <summary>
''' 快捷键
''' </summary>
Public Property Hotkey() As Keys
Get
Return KeysConverter.ConvertFromString(gComIWshShortcut.Hotkey)
End Get
Set(ByVal value As Keys)
gComIWshShortcut.Hotkey = KeysConverter.ConvertTo(value, GetType(String))
End Set
End Property
''' <summary>
''' 图标位置
''' </summary>
Public Property IconLocation() As String
Get
Return gComIWshShortcut.IconLocation
End Get
Set(ByVal value As String)
gComIWshShortcut.IconLocation = value
End Set
End Property
''' <summary>
''' 相对路径
''' </summary>
Public WriteOnly Property RelativePath() As String
Set(ByVal value As String)
gComIWshShortcut.RelativePath = value
End Set
End Property
''' <summary>
''' 运行方式
''' </summary>
Public Property WindowStyle() As WshWindowStyle
Get
Return gComIWshShortcut.WindowStyle
End Get
Set(ByVal value As WshWindowStyle)
gComIWshShortcut.WindowStyle = value
End Set
End Property
''' <summary>
''' 起始位置
''' </summary>
Public Property WorkingDirectory() As String
Get
Return gComIWshShortcut.WorkingDirectory
End Get
Set(ByVal value As String)
gComIWshShortcut.WorkingDirectory = value
End Set
End Property
Private Sub OnlyMeDispose()
If gComIWshShortcut Is Nothing Then Return
System.Runtime.InteropServices.Marshal.ReleaseComObject(gComIWshShortcut)
gComIWshShortcut = Nothing
End Sub
''' <summary>
''' 释放内存
''' </summary>
Public Sub Dispose() Implements System.IDisposable.Dispose
OnlyMeDispose()
System.Runtime.InteropServices.Marshal.ReleaseComObject(gComIWshShell3)
gComIWshShell3 = Nothing
End Sub
''以下为补充
''' <summary>
''' 默认图标位置
''' </summary>
Public Function DefaultIcon() As String
Return "%SystemRoot%system32SHELL32.dll,30"
End Function
Public Sub SetDefaultIcon()
Me.IconLocation = Me.DefaultIcon
End Sub
''' <summary>
''' 获取常用目录
''' </summary>
Public Function SpecialFolder(ByVal folder As Environment.SpecialFolder) As String
Return Environment.GetFolderPath(folder)
End Function
''' <summary>
''' 在指定目录下打开或准备创建快捷方式
''' </summary>
''' <param name="folder">目录</param>
''' <param name="name">快捷方式名称</param>
''' <remarks>如要创建或修改,配置参数后需要保存</remarks>
Public Shared Function CreateIn(ByVal folder As Environment.SpecialFolder, ByVal name As String) As WshShortcut
Dim mShortcut As New WshShortcut
Dim mPathLink As String = String.Concat(mShortcut.SpecialFolder(folder), "\", name)
mShortcut.Create(mPathLink)
Return mShortcut
End Function
Private Shared KeysConverter As New KeysConverter
End Class
End Namespace
Namespace LzmTW.uSystem.uIO
Public Enum WshWindowStyle
'WshHide = 0
WshNormalFocus = 1
'WshMinimizedFocus = 2
WshMaximizedFocus = 3
'WshNormalNoFocus = 4
'WshMinimizedNoFocus = 6
WshMinimizedFocus = 7
End Enum
End Namespace
相关文章推荐
- Windows 中如何创建一个指向某相对路径的快捷方式(即快捷方式的目标路径为某相对路径)
- android:在一个应用中创建其它应用的快捷启动方式Launcher
- Android_如何为一个app创建桌面快捷方式
- C# 创建一个application的桌面快捷方式
- 一个apk多个入口,在桌面上创建多个应用图标的桌面快捷方式
- 创建一个远程桌面连接的快捷方式
- windows -- 怎么创建一个网页快捷方式
- 开机默认设置一个widge在指定的屏幕上和自动在桌面上创建一个应用的快捷方式的方法
- C#创建一个文件的快捷方式
- bat脚本实例实现只允许运行一个实例(安装程序、创建快捷方式脚本)
- CAD常见问题之创建一个新的键盘快捷方式
- 创建一个用于建立快捷方式的COM 对象并取得IShellLink 接口。
- C#创建一个文件的快捷方式
- Ubuntu下如何在桌面创建一个应用程序的“快捷方式”?
- Ubuntu下如何在桌面创建一个桌面快捷方式
- C# 创建一个文件夹快捷方式
- ACTION_CREATE_SHORTCUT (接受此广播的Receiver会创建一个快捷方式)
- ShortCut的延伸功能--为一个app的不同界面创建不同的快捷方式
- 在liunx中创建启动器(window下的快捷方式) && linux下,如何建立一个“关闭屏幕”的启动器 ?
- “目录2”挂载“目录1”的方法:也就是“目录2”下创建一个“目录1”的快捷方式的方法