The .NET way to create shortcuts (ShellLink)

In a recent blog entry, Duncan pointed out a way to create a shell link (shortcut) using windows scripting host.  If you ask me though, that is a pretty chunky approach as it means you have a COM wrapper for WSH, and WSH itself is just wrappers for the calls to the underlying system API.  So, it’s layer upon layer upon layer.  That’s good if it’s a toblerone, but not so good for efficient code.

A much cooler approach is to use the very nice .NET wrapper classes Mattias Sjögren provided a few years back.   The wrappers and samples are written in both VB.NET and C#.

And the even better news is that in Whidbey, the COMImport attribute bug has been fixed so the work-arounds Mattias used in VB.NET such as Activator.CreateInstance won’t be needed.

In any case, it’s great code, grab it today (oh and avoid using WSH for this)

4 Comments so far

  1.   Anonymous on October 7th, 2004          

    Bill, please be mindful that everything that is not Win32 API is essentially a wrapper; including .NET CLR (probably until the release of Longhorn). I can’t see that much overhead in the interaction between Scripting runtime and COM objects instantiated during the execution of a script.

    Besides, do you really need that much efficiency for creating just a shortcut? WSH is widely available on almost all default Windows installation, while .NET is not. So that’s definitely a big plus.

    Don’t you think it’s really that efficient to waste hundreds of megs for .NET framework installation, when all you need is just automating the creation of a bloody shorcut? Read Brendon Chase editorial in Builder AU’s October 2004 edition, you probably need to renew your state of mind and not get too carried away by only a specific tool just because you think you know it best. šŸ˜‰ Good luck, Bill!

  2.   [rux] on October 14th, 2004          

    You mean I have to download 22MB of .NET framework just to create a wrapper for a shortcut ? Hmmmmm .. sounds justifying.

    .NET is good, but it’s not the solution for every problems.

  3.   Andrei on June 9th, 2005          

    22KB, not 22MB.

  4.   tracernet on December 2nd, 2005          

    its not working when i run it using VS.NET 2003…