Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

Windows shortcuts are .LNK files – a proprietary binary format which is not simple to generate. If you want to create shortcuts as part of an MSBuild deployment, the main issue is that the paths are not relative, they need to be explicit, so you can't create your own shortcuts and copy them. Alternatives are to create .URL or batch files containing relative paths, but they don't have the niceties of shortcuts.

You can generate .LNK shortcuts in MSBuild by shelling out to some VBScript at runtime, when you've deployed your files and know the exact paths. It's not a pretty solution, but it's fast and reliable. Create a .VBS file with the following content to generate a shortcut from given parameters:

'CreateShortcut.vbs

'Generates a .LNK shortcut to the specified program

'Usage: CreateShortcut.vbs <pathToExe> <exeName> <shortcutName>

 

Dim args, arg

Set args = WScript.Arguments

 

Dim workingDirectory, exeName, shortcutName

workingDirectory=args(0)

exeName=args(1)

shortcutName=args(2)

 

Dim WshShell

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

 

Dim objLink

Set objLink = WshShell.CreateShortcut("Shortcuts\" & shortcutName & ".lnk")

objLink.Description = shortcutName

objLink.TargetPath = workingDirectory & "\" & exeName

objLink.WindowStyle = 1

objLink.WorkingDirectory = workingDirectory

objLink.Save

Then call it from MSBuild after you've deployed the target file and know the path:

<Exec Command='CreateShortcut.vbs """$(AppDir)""" AppName.exe AppFriendlyName.exe'/>

Points to note:

  • If you have any spaces in the arguments (C:\Program Files\etc) you need to wrap them in triple double-quotes ("""C:\Program Files\etc"""), otherwise each word is parsed as a separate argument in VBScript;
  • The friendly name of the shortcut can end in any extension – Windows hides the final .LNK , so if you specify a .EXE it will display as an EXE;
  • Shortcuts aren't limited to executables, if you pass a Word file as the exeName argument and end the shortcutName with .DOC, it'll launch the document in Word and show it with the Word icon in Explorer;
  • You have a lot of control over the link from the CreateShortcut call – including the startup window type, arguments and the icon. The example above is a straightforward one which just sets the minimum necessary.
Posted on Friday, October 24, 2008 7:13 PM Scripting & Automation , Code Snippet , MSBuild | Back to top


Comments on this post: Creating Shortcuts in MSBuild

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
Wow, great answer to fix my bug, i've got space in my arguments and i did not understand why did it bug ? That post makes me realize my mistake, thx for that helpful resource !
Left by typy bukmacherskie on Aug 26, 2009 4:39 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
If you want to create shortcuts as part of an MSBuild deployment, the main issue is that the paths are not relative, they need to be explicit, so you can't create your own shortcuts and copy them.Thanks.
Left by seo experts on Oct 19, 2010 7:41 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
I have searched in the internet to find an easy way to rebuild my visual studio solution without open it. Then I find there is a way to update the registry to provide a context shortcut to run msbuild command by right-click on .sln file from.
Left by ultrasound technician schools on Nov 03, 2010 2:27 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
During the course of development, it's important to have an automated build process. Equally important is having an automated means of creating releases.
Left by digital scrapbook on Nov 09, 2010 8:38 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
MSBuild is the build system for Visual Studio. The MSBuild project file format enables developers to fully describe what items need to be built. It is a significant progress for Visual Studio. I am searching an easy way to rebuild my visual studio solution without open it. I searched in the internet, and then I got a way to update the registry to provide a context shortcut to run msbuild command by right-click on .sln file.
Left by Digital Scrapbooking on Dec 08, 2010 2:57 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
The .NET 2 platform is delivered with a new tool named msbuild.exe. This tool is used to build .NET applications. It accepts XML files which describe the sequence of tasks for the build process. Thank you for the information about creating the shortcut of msbuild. This is exactly what I was looking for.
Left by driving lessons in Milton Keynes on Mar 03, 2011 5:33 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
Thanks for this post.I would like to read more about your articles.That contains nice information....Thanks.
Left by Fly Ash Transportation on Mar 09, 2011 11:08 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
MSBuild is a significant progress for Visual Studio. I am searching an easy way to rebuild my visual studio solution without open it. Hope your tips will be helpful for me.
Left by article submission services on Mar 26, 2011 3:50 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
Absolutely, shortcuts cannot use relative address as this can makes user confused when use the shortcut.
Left by law degree online on May 13, 2011 7:23 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
You gave tremendous positive points there. I did a search on the topic and found most peoples will agree with your blog.
Left by Solidworks on Jul 15, 2011 6:17 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
MSBuild is the build system for Visual Studio. The MSBuild project file format enables developers to fully describe what items need to be built. It accepts XML files which describe the sequence of tasks for the build process.
Left by Heathrow Taxi on Jul 28, 2011 6:02 AM

# re: Creating Shortcuts in MSBuild
Requesting Gravatar...
Stunning, incredible response to settle my bug, i have space in my contentions and i didn't comprehend why did it bug ? That post commits me understand my error, thanx for that supportive asset ! Article Rewriter Pro
I have looked in the web to locate a simple approach to revamp my visual studio arrangement without open it. At that point I find there is an approach to upgrade the registry to give a setting alternate route to run msbuild summon by right-tap on .sln document from.
Left by Mahira Khan Biography on Jan 07, 2017 3:09 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net