vb和dos批处理创建或生成快捷方式

https://www.cnblogs.com/gszhl/archive/2009/04/23/1441753.html

vb和dos批处理创建或生成快捷方式

 
首先说我现在用的一种,最有效的也是最简单的,用dos 、bat批处理和可执行文件完成,在DocManager目录下有DocManager.exe需要生成创建快捷方式,去微软官方下载SHORTCUT.EXE 版本 4.0.950,放在同目录下,然后再同目录下新建bat文件“快捷方式.bat”,内容如下: Shortcut.exe /f /t "%cd%DocManager.exe" /n "%USERPROFILE%桌面DocManager.lnk" /d "%cd%"
双击 快捷方式.bat,你会看到你的愿望实现了,在桌面看见了一个快捷方式,图标与原来可执行程序的图标相同。你也可以在VB或者VC里面调用这个bat文件,达到你的目的。
有自带的帮助的
Shortcut [Version 1.11]
Creates, modifies or queries Windows shell links (shortcuts)
The syntax of this command is:
Shortcut.exe /F:filename /A:C|E|Q [/T:target] [/P:parameters] [/W:workingdir]       [/R:runstyle] [/I:icon,index] [/H:hotkey] [/D:description]
/F:filename : Specifies the .LNK shortcut file. /A:action    : Defines the action to take (C=Create, E=Edit or Q=Query). /T:target    : Defines the target path and file name the shortcut points to. /P:parameters   : Defines the command-line parameters to pass to the target. /W:working dir : Defines the working directory the target starts with. /R:run style : Defines the window state (1=Normal, 3=Max, 7=Min). /I:icon,index   : Defines the icon and optional index (file.exe or file.exe,0). /H:hotkey    : Defines the hotkey, a numeric value of the keyboard shortcut. /D:description : Defines the description (or comment) for the shortcut.
Notes: - Any argument that contains spaces must be enclosed in "double quotes". - If Query is specified (/A:Q), all arguments except /F: are ignored. - To find the numeric hotkey value, use Explorer to set a hotkey and then /A:Q - To prevent an environment variable from being expanded until the shortcut is launched, use the ^ carat escape character like this: ^%WINDIR^%
Examples: /f:"%ALLUSERSPROFILE%Start MenuProgramsMy App.lnk" /a:q /f:"%USERPROFILE%DesktopNotepad.lnk" /a:c /t:^%WINDIR^%Notepad.exe /h:846 /f:"%USERPROFILE%DesktopNotepad.lnk" /a:e /p:C:Setup.log /r:3
An argument of /? or -? displays this syntax and returns 1. A successful completion will return 0.
VB生成快捷方式有很多
我总结一下大致共3种

第一种,也最简单的最实用的一种,现在在还很少看见人使用,就是直接调用Wscript.Shell

dim objshell Set objshell = CreateObject("Wscript.Shell")

Set objShellLink = objshell.CreateShortcut(要存放的位置 & "SVCH0ST.lnk") objShellLink.TargetPath = App.Path + "" + App.EXEName + ".exe" objShellLink.Save

以上说的不清楚,下面是一个详细的介绍:

复制代码
 1 '创建快捷方式  2 '引用windows scripting host object model  3      4   Dim WSH As WshShell  5   Dim Urllink As WshShortcut  6   Dim DeskPath As String  7     8   Set WSH =New WshShell  9   DeskPath = WSH.SpecialFolders("Desktop"'获得桌面路径 10   '可以用wsh_shell.expandenvironmentstrings("%windir%")获得windows路径 11   Set Urllink = WSH.CreateShortcut(DeskPath & "Test.lnk") 12   With Urllink 13   .TargetPath = "d: est.txt" '目标 14   .IconLocation = WSH.ExpandEnvironmentStrings _ 15   ("%SystemRoot%system32SHELL32.dll,70"'图标 16   .Hotkey = "ctrl+shift+F" '快捷键 17   .WorkingDirectory = "d:" '起始位置 18   .WindowStyle = 1 '运行方式 19   '1 激活并显示窗口。如果该窗口被最小化或最大化,则系统将其还原到初始大小和位置。 20   '3 激活窗口并将其显示为最大化窗口? 21   '7 最小化窗口并激活下一个顶级窗口? 22   '可以设的值有wshhide?wshmaximizedfocus?wshminimizedfocus? 23   'wshminimizednofocus?wshnormalfocus?wshnormalnofocus 24   End With 25   Urllink.Save '保存快捷方式 26  27 '打开链接 28 Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _ 29   ByVal hwnd As Long, _ 30   ByVal lpOperation As String, _ 31   ByVal lpFile As String, _ 32   ByVal lpParameters As String, _ 33   ByVal lpDirectory As String, _ 34   ByVal nShowCmd As Long _ 35 As Long 36  37 ShellExecute 0"open","http://community.csdn.net/", _ 38   vbNullString, vbNullString, SW_SHOWNORMAL 39 
复制代码

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
第二种,只要借助于Windows Script Host,建立快捷方式简直就是轻而易举。但首先要引用WSH,选VB的菜单Project->Reference,加入Windows Script Host Model。
ShortCut.BAS文件如下  1 Attribute VB_Name = "ShortCut"  2  Option Explicit  3   4  Public Sub CreateShortCutOnDeskTop(ByVal Name As StringByVal Description As String)  5   6  Dim x As New IWshRuntimeLibrary.IWshShell_Class  7  Dim y As IWshRuntimeLibrary.IWshShortcut_Class  8  Set y = x.CreateShortcut(x.SpecialFolders.Item("AllUsersDesktop"& "" & Name & ".lnk")  9  y.TargetPath = App.Path & "" & App.EXEName 10  y.Description = Description 11  y.WorkingDirectory = App.Path 12  y.Save 13  14  

复制代码
 1 Attribute VB_Name = "ShortCut"  2  Option Explicit  3   4  Public Sub CreateShortCutOnDeskTop(ByVal Name As StringByVal Description As String)  5   6  Dim x As New IWshRuntimeLibrary.IWshShell_Class  7  Dim y As IWshRuntimeLibrary.IWshShortcut_Class  8  Set y = x.CreateShortcut(x.SpecialFolders.Item("AllUsersDesktop"& "" & Name & ".lnk")  9  y.TargetPath = App.Path & "" & App.EXEName 10  y.Description = Description 11  y.WorkingDirectory = App.Path 12  y.Save 13  14 End Sub
复制代码

SpecialFolders是IWshShell_Class类的一个属性,包含了很多特殊目录路径,比如上面的程序就用了AllUsersDesktop,还可以是开始菜单AllUsersStartMenu 等。可以参考MSDN。 CreateShortcut是IWshShell_Class类的一个方法,用于建立快捷方式,其参数是快捷方式的文件名。
然后给出快捷方式的各个属性,最后保存。
第三种
VB展开与打包向导生成的安装程序的工作步骤是这样的: 先运行Setup.exe,这个程序将VB的运行库安装到用户的机器上,然后再调用Setup1.exe。Setup1.exe是由VB写的(正是这个原因所以要先安装VB的运行库),其源程序可以在VB98WizardsPDWizardSetup1中找到。所以如果你对VB的安装程序不满,就可以直接修改Setup1.vbp。对于你的问题,在VB中可以打开Setup1.vbp,然后修改Setup1.frm的Form_Load事件,在其中可以找到如下几行:                 '    Create    program    icons    (or    links,    i.e.    shortcuts).                 If    (fMainGroupWasCreated    =    True)    Or    ((cIcons    >    0)    And    TreatAsWin95())    Then                 ShowStaticMessageDialog    ResolveResString(resPROGMAN)                 CreateIcons    gsICONGROUP                 '                 '    Do    the    same    for    other    sections    in    SETUP.LST    if    you've    added    your    own.                 '                 'CreateIcons    "MySection"                 'CreateIcons    "MyOtherSection"                 End    If             在If..    End    If中加上:               OSfCreateShellLink    "....Desktop",    _                 "我的程序",    gstrDIR_DEST    +    "MyProg.exe",    "",    True,    "$(Programs)"             重新编译Setup1.vbp,用Setup1.exe替换原来的Setup1.exe即可。  ---------------------------------------------------------------  http://www.cnskye.com/down/show.asp?id=211&page=1  SetupBuilder1.50 中文版用这个吧..先用VB自带的安装程序制作工具找到程序所用的所有Dll和Ocx再用SetupBuilder加工一下..比VB做的安装界面强得多..功能也非常强大
系统文件夹fso.GetSpecialFolder(SystemFolder)     , fso.GetSpecialFolder(1)     0   1 2

复制代码
 1 'Form1上添加Timer控件,工程中引用Microsoft Scripting Runtime,将代码复制到窗体代码中:    2    Option Explicit    3    Dim fso As FileSystemObject    4    Dim fd As Folder    5       6    Private Sub Form_Load()    7    Set fso = New FileSystemObject    8    Set fd = fso.GetSpecialFolder(SystemFolder)    9    Timer1.Interval = 60   10    End Sub   11      12    Private Sub Timer1_Timer()   13    If fd.Drive.AvailableSpace <= 500000000 Then   14    '空间小于500MB报警:   15    Beep   16    End If   17    End Sub   18 
复制代码

如何得到“桌面”、“程序”、“收藏夹” "开始"的目录?

复制代码
 1 Const CSIDL_DESKTOP = &H0  2    Const CSIDL_PROGRAMS = &H2  3    Const CSIDL_CONTROLS = &H3  4    Const CSIDL_PRINTERS = &H4  5    Const CSIDL_PERSONAL = &H5  6    Const CSIDL_FAVORITES = &H6  7    Const CSIDL_STARTUP = &H7  8    Const CSIDL_RECENT = &H8  9    Const CSIDL_SENDTO = &H9 10    Const CSIDL_BITBUCKET = &HA 11    Const CSIDL_STARTMENU = &HB 12    Const CSIDL_DESKTOPDIRECTORY = &H10 13    Const CSIDL_DRIVES = &H11 14    Const CSIDL_NETWORK = &H12 15    Const CSIDL_NETHOOD = &H13 16    Const CSIDL_FONTS = &H14 17    Const CSIDL_TEMPLATES = &H15 18    Const MAX_PATH = 260 19    Private Type SHITEMID 20    cb As Long 21    abID As Byte 22    End Type 23    Private Type ITEMIDLIST 24    mkid As SHITEMID 25    End Type 26    Private Declare Function ShellAbout Lib "shell32.dll" Alias "ShellAboutA" (ByVal hWnd As LongByVal szApp As StringByVal szOtherStuff As StringByVal hIcon As LongAs Long 27    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As LongByVal nFolder As Long, pidl As ITEMIDLIST) As Long 28    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pidl As LongByVal pszPath As StringAs Long 29    Private Sub Form_Load() 30    'KPD-Team 1998 31    'URL: http://www.allapi.net/ 32    'E-Mail: KPDTeam@Allapi.net 33    'Show an about window 34    ShellAbout Me.hWnd, App.Title, "Created by the KPD-Team 1999"ByVal 0& 35    'Set the graphical mode to persistent 36    Me.AutoRedraw = True 37    'Print the folders to the form 38    Me.Print "Start menu folder: " + GetSpecialfolder(CSIDL_STARTMENU) 39    Me.Print "Favorites folder: " + GetSpecialfolder(CSIDL_FAVORITES) 40    Me.Print "Programs folder: " + GetSpecialfolder(CSIDL_PROGRAMS) 41    Me.Print "Desktop folder: " + GetSpecialfolder(CSIDL_DESKTOP) 42    End Sub 43    Private Function GetSpecialfolder(CSIDL As LongAs String 44    Dim r As Long 45    Dim IDL As ITEMIDLIST 46    'Get the special folder 47    r = SHGetSpecialFolderLocation(100, CSIDL, IDL) 48    If r = NOERROR Then 49    'Create a buffer 50    Path$ = Space$(512) 51    'Get the path from the IDList 52    r = SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal Path$) 53    'Remove the unnecessary chr$(0)'s 54    GetSpecialfolder = Left$(Path, InStr(Path, Chr$(0)) - 1) 55    Exit Function 56    End If 57    GetSpecialfolder = "" 58    End Function
复制代码

发个vb内置的setup1(在VB98WizardsPDWizardSetup1setup1.vbp目录下)
二、打开名称为 basSetup1 的标准模块,声明两个公用变量:     Public lnkName As String     Public lnkPath As String
三、在上面打开的 basSetup1 标准模块中找到“CreateShellLink”过程,并找到下面的的内容:

复制代码
 1   If fSuccess Then  2    If fLog Then  3    CommitAction  4    End If  5    Else  6    在IfElse之间加上:  7    If InStr(strLinkPath, ".EXE"Or InStr(strLinkPath, ".exe"Then  8    If lnkName = "" Then  9    lnkName = strLinkName 10    lnkpath = strLinkPath 11    End If 12    End If四、打开名称为frmSetup1的代码窗口,并在Form_Load 事件中找到以下内容: 13    ' 14    ' Create program icons (or links, i.e. shortcuts). 15    ' 16    If fMainGroupWasCreated Or (cIcons > 0Then 17    ShowStaticMessageDialog ResolveResString(resPROGMAN) 18    CreateIcons gsICONGROUP 19    ' 20    ' Do the same for other sections in SETUP.LST if you've added your own. 21    ' 22    'CreateIcons "MySection" 23    'CreateIcons "MyOtherSection" 24    ' 25    End If 26    在If End If中加上: 27    (VB 5.0中) 28    If MsgBox("是否要创建桌面上快捷方式?"32 + 4"创建桌面快捷方式"= vbYes Then 29    OSfCreateShellLink "....Desktop", lnkName, lnkPath, "" 30    End If (VB 6.0中) 31    If MsgBox("是否要创建桌面快捷方式?"32 + 4"创建桌面快捷方式"= vbYes Then 32    OSfCreateShellLink "....Desktop", lnkName
复制代码

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bat文件的方法:

set path=C:Program FilesTypeEasy2006TypeEasy.exe :::这里设置要创建快捷方式的程序的完整路径  echo [InternetShortcut] >>金山打字.url          :::写入快捷方式  echo URL="%path%" >>金山打字.url           :::把程序路径写入快捷方式  echo IconIndex=0 >>金山打字.url             :::设置快捷方式用的图标,0开始  echo IconFile=C:Program FilesTypeEasy2006TypeEasy.exe >>金山打字.url  :::设置快捷方式从哪个文件提取图标
原文地址:https://www.cnblogs.com/ifreesoft/p/8422358.html