29

I have been looking for a simple way to create a shortcut to a file in C#, but I've only found external dlls that do that. It's actually quite surprising, there's no built in way to do that..

Anyway, I know that lnk files are just text files with a certain command and a given path. I thought that maybe I could create a text file (in the code) set it's text to the right command and change it's extension to .lnk I've tried to do that manually first, but failed to do so.

Is there a way to do something like that (or maybe another simple way) to create a shortcut to a certain path in c#?

Just to be clear, by Shortcut I mean an .lnk file that leads to the file Edit: And by file I mean any file I'd want, not only a shortcut to my own application


I'll edit if it doesn't work well for every scenario.

Add these references:

  1. Microsoft Shell Controls And Automation
  2. Windows Script Host Object Model

Add this namespaces:

using Shell32;
using IWshRuntimeLibrary;

Next appears to be working:

var wsh = new IWshShell_Class();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut2.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = @"C:\Users\Zimin\Desktop\test folder";            
shortcut.Save();

Hope it helps others as well, thanks for your attention.

Also, if there IS a way to create a file, write the right commands and then change it to an lnk file, please let me know.

Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34
user2599803
  • 299
  • 1
  • 3
  • 7
  • Is this a winforms app? If so, this could be a duplicate question http://stackoverflow.com/questions/1501608/how-do-you-create-an-application-shortcut-lnk-file-in-c-sharp-with-command-li And http://stackoverflow.com/questions/234231/creating-application-shortcut-in-a-directory – user1477388 Aug 02 '13 at 18:06
  • Also, see this http://social.msdn.microsoft.com/Forums/windows/en-US/e0ac908d-8c33-4d99-88dd-d8d8be77d198/how-to-create-a-decktop-icon-for-exe-file-in-c-windows-application-through-setup – user1477388 Aug 02 '13 at 18:06
  • There is no newer way since the linked answer as far as I know. On asking question: avoid tags in title and "thank you notes", feel free to discuss on [meta](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – Alexei Levenkov Aug 02 '13 at 18:11
  • Do you want to create shortcut of you application or some other file? – Ehsan Aug 02 '13 at 18:16
  • " I know that lnk files are just text files with a certain command and a given path." No, that's not true at all. A .lnk is a binary file. See http://msdn.microsoft.com/en-us/library/dd871305.aspx. Did you do any research before posting your question? – Jim Mischel Aug 02 '13 at 19:55
  • It's not a winforms app (tho I will probably work on GUI later). I've read these. One of them requires an external dll if I got it right. And it seems quite a lot of work for something so simple to do... – user2599803 Aug 02 '13 at 21:18
  • And yes I did my research and someone (I think on stackoverflow, but not sure) said those were just text files with commands. I've even found the command line that should do the trick, but it doesn't.. I've also tried working with one of the examples given by user1477388 but even after adding the references my vs doesn't recognize Shell32 and IWshRuntimeLibrary.. – user2599803 Aug 02 '13 at 21:53
  • I can’t believe it took me this long to find an answer that didn’t require using an external dll. Nor can I believe this isn’t more accessible in System.IO or something. – RTHarston Dec 03 '21 at 20:59

1 Answers1

23

One way to do this is pointed out by Joepro in their answer here:

You'll need to add a COM reference to Windows Scripting Host. As far as i know, there is no native .net way to do this.

WshShellClass wsh = new WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.Arguments = "";
shortcut.TargetPath = "c:\\app\\myftp.exe";
// not sure about what this is for
shortcut.WindowStyle = 1; 
shortcut.Description = "my shortcut description";
shortcut.WorkingDirectory = "c:\\app";
shortcut.IconLocation = "specify icon location";
shortcut.Save();

For .Net 4.0 and above, replace the first line with the following:

 WshShell wsh = new WshShell();

EDIT: This link can help too

Danilo Breda
  • 1,092
  • 1
  • 15
  • 30
  • 1
    This seems to be ok, and I have seen this already.. I just had something else in mind... But if this allows me to do that and there's no better way, I'll try and use it i guess. – user2599803 Aug 02 '13 at 21:23
  • 3
    Also "Embed Interop Types" should be set to false when referencing the Windows Scripting Host. Otherwise you'll get The type 'IWshRuntimeLibrary.WshShellClass' has no constructors defined – Halil Dec 01 '14 at 18:11
  • 4
    Add Reference > COM > Windows Script Host Object Model – Doug Jan 21 '15 at 03:37