Easy Thumbnails
Written by Harry Fairhead   
Friday, 31 July 2009
Article Index
Easy Thumbnails
Thumber
Creating the shortcut
The UI
Converting the list
Improving the thumbnails

Creating the shortcut

Now we are ready to create the shortcut – but there is a problem.

Even though there are lots of helpful classes in the .NET Framework there doesn’t seem to be one that lets you work with shortcuts. There are a number of solutions to this problem including using pInvoke to call low-level API functions but arguably the simplest is to use and ActiveX class library that is generally available as part of the Scripting support (for both JScript and VBScript).

To use it first load a reference to a COM object - Windows Script Host Object – and add:

using IWshRuntimeLibrary;

Now we can create a shortcut in exactly the same way that we would using VBScript or JScript. First create a WhsShell object:

WshShellClass WshS = 
new WshShellClass();

Use this to create a shortcut object:

IWshShortcut Shortcut= 
(IWshShortcut)WshS.CreateShortcut(
SendToDir+@"\Thumber.lnk");

Notice that we have to specify the name and location of the new shortcut.

Finally we customise the shortcut object using its properties:

WshShellClass WshS = 
new WshShellClass();
IWshShortcut Shortcut=
(IWshShortcut)WshS.CreateShortcut(
SendToDir+@"\Thumber.lnk");
Shortcut.TargetPath =
ProgramPath+@"\thumber.exe";
Shortcut.WindowStyle = 1;

There are other properties that you can set, e.g. a custom icon, but these are all we need in this case. Notice that the shortcut is called Thumber.lnk. Finally we save the Shortcut object and this is the command that actually creates the link:

 Shortcut.Save();
}

If the link already exists it is deleted and re-written. There probably will be a managed way to create a shortcut in a future version of the .NET Framework and when there is it will be the method of choice.

<ASIN:0071614060>

<ASIN:1598638068>

<ASIN:0672328917>



Last Updated ( Friday, 31 July 2009 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.