2

Possible Duplicate:
How do you create an application shortcut (.lnk file) in C# or .Net

Hi,

Any clue on how to create shortcut for an .exe in C#?

Thanks

Community
  • 1
  • 1
Jayesh
  • 3,891
  • 11
  • 55
  • 83
  • 4
    Have you tried Google? http://www.google.com/search?q=c%23+create+shortcut, dozens of results. – Andy E Jun 12 '10 at 15:31

2 Answers2

3

I found this answer in google, at: http://www.geekpedia.com/tutorial125_Create-shortcuts-with-a-.NET-application.html

Just:

WshShell = new WshShellClass();

// Create the shortcut
IWshRuntimeLibrary.IWshShortcut MyShortcut;

// Choose the path for the shortcut
MyShortcut = IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(@"C:\MyShortcut.lnk");

// Where the shortcut should point to
MyShortcut.TargetPath = Application.ExecutablePath;

// Description for the shortcut
MyShortcut.Description = "Launch My Application";

// Location for the shortcut's icon
MyShortcut.IconLocation = Application.StartupPath + @"\app.ico";

// Create the shortcut at the given path
MyShortcut.Save();

Just remember to add the reference Windows Script Host Object Model

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68
  • 1
    This is the same answer given in the [duplicate question](http://stackoverflow.com/questions/234231/how-do-you-create-an-application-shortcut-lnk-file-in-c-or-net). – Andy E Jun 12 '10 at 15:39
  • @Andy This is *not* the same answer given in the duplicate thread. The other thread uses the proper API - The ShellLink COM object, which someone wrapped in a managed wrapper. – Ian Boyd Jun 12 '10 at 16:16
  • @Ian: Maybe I should have linked right to [the duplicate answer](http://stackoverflow.com/questions/234231/how-do-you-create-an-application-shortcut-lnk-file-in-c-or-net/234243#234243). The only difference here is that code was pasted in from the linked site. – Andy E Jun 12 '10 at 19:08
-2

Here's my Delphi code:

function CreateShellLink(const szFilename: string; const szDescription: string): IShellLinkA;
var
    sl: IShellLinkA;
begin
    sl := CreateComObject(CLSID_ShellLink) as IShellLinkA;
    OleCheck(sl.SetPath(szFilename));
    OleCheck(sl.SetDescription(szDescription));

    Result := sl;
end;

You'll have to figure out using Win32 api from .NET.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219