Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a Shortcut

6 views
Skip to first unread message

Jeff Gaines

unread,
Nov 4, 2002, 11:17:36 AM11/4/02
to

I want to be able to create a shortcut (.lnk file) programmatically in
C# from a file (or folder) being dragged to a ListView.

In VB I would have used Scripting and created a Scripting object. I am
unable to do this in C#, can anybody give me a nudge in the right
direction please? If there's a better way of doing it in C# I would be
happy to try it.

--
Jeff Gaines Damerham Hampshire UK
je...@jgaines.co.uk

Felix Wu [MS]

unread,
Nov 5, 2002, 8:30:04 AM11/5/02
to
Hi Jeff,

Since .NET does not provide one-step method to create shortcut, you need to
do it via InterOp. Here is a code smaple that creates a shortcut by using
"Windows Script Host Object Model" COM library:

using System;
using System.Runtime.InteropServices;
using IWshRuntimeLibrary;

namespace CreateShortcutCOM {
/// <summary>
/// This class creates a shortcut with COM interoperability
/// </summary>
class ShortcutDemo {
[STAThread]
static void Main(string[] args) {
// Get the app path and filename
string app = Environment.CurrentDirectory + @"\CreateShortcutCOM.exe";
try {
// Create a Windows Script Host Shell class
IWshShell_Class shell = new IWshShell_ClassClass();
// Define the shortcut file
IWshShortcut_Class shortcut = shell.CreateShortcut(app + ".lnk") as
IWshShortcut_Class;
// Set all its properties
shortcut.Description = "Smart sample of creating shell shortcut";
shortcut.TargetPath = app;
shortcut.IconLocation = app + ",0";
// Save it
shortcut.Save();
}
catch(COMException ex) {
Console.WriteLine(ex.Message);
}
}
}
}

To make the above code comiple, do not forget to add a reference to the COM
library wshom.ocx

Hope this helps.

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: Jeff Gaines <je...@jgaines.co.uk>
>Newsgroups: microsoft.public.dotnet.languages.csharp
>Subject: Creating a Shortcut
>Date: Mon, 04 Nov 2002 16:17:36 +0000
>Lines: 12
>Message-ID: <u77dsu85a956sispa...@4ax.com>
>Reply-To: je...@jgaines.co.uk
>NNTP-Posting-Host: host213-122-10-129.in-addr.btopenworld.com
(213.122.10.129)
>Mime-Version: 1.0
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>X-Trace: fu-berlin.de 1036426626 7099449 213.122.10.129 (16 [120840])
>X-Newsreader: Forte Agent 1.92/32.572
>Path:
cpmsftngxa09!tkmsftngxs02!tkmsftngp01!newsfeed00.sul.t-online.de!newsfeed01.
sul.t-online.de!t-online.de!fu-berlin.de!uni-berlin.de!host213-122-10-129.in
-addr.btopenworld.COM!not-for-mail
>Xref: cpmsftngxa09 microsoft.public.dotnet.languages.csharp:105337
>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

Jeff Gaines

unread,
Nov 6, 2002, 4:18:07 AM11/6/02
to
On Tue, 05 Nov 2002 13:30:04 GMT, fel...@online.microsoft.com (Felix
Wu [MS]) wrote:

>Hi Jeff,
>
>Since .NET does not provide one-step method to create shortcut, you need to
>do it via InterOp. Here is a code smaple that creates a shortcut by using
>"Windows Script Host Object Model" COM library:

[snipped]

Felix

That's brill, thanks very much!

I could do with a book on getting into the more esoteric bits of
Windows and the API, any suggestions?

Regards.

Felix Wu [MS]

unread,
Nov 6, 2002, 5:04:13 AM11/6/02
to
Hi Jeff,

I think the following articles in the MSDN is good start point:

Interoperating with Unmanaged Code
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconinteroperatingwith
unmanagedcode.asp

Regards,

Felix Wu
=============
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: Jeff Gaines <je...@jgaines.co.uk>
>Newsgroups: microsoft.public.dotnet.languages.csharp

>Subject: Re: Creating a Shortcut
>Date: Wed, 06 Nov 2002 09:18:07 +0000
>Lines: 19
>Message-ID: <jenhsuc7leo0pkmb0...@4ax.com>
>References: <u77dsu85a956sispa...@4ax.com>
<xEt1b9MhCHA.2448@cpmsftngxa09>
>Reply-To: je...@jgaines.co.uk
>NNTP-Posting-Host: host213-122-167-107.in-addr.btopenworld.com
(213.122.167.107)


>Mime-Version: 1.0
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit

>X-Trace: fu-berlin.de 1036574253 8362533 213.122.167.107 (16 [120840])


>X-Newsreader: Forte Agent 1.92/32.572
>Path:

cpmsftngxa09!cpmsftngxa06!tkmsftngp01!cppssbbsa01.microsoft.com!news-out.cwi
x.com!newsfeed.cwix.com!news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berl
in.de!uni-berlin.de!host213-122-167-107.in-addr.btopenworld.COM!not-for-mail
>Xref: cpmsftngxa09 microsoft.public.dotnet.languages.csharp:105828
>X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

0 new messages