Click here to Skip to main content
13,055,754 members (48,371 online)
Rate this:
Please Sign up or sign in to vote.
See more:
What I desire to do is that in a click of a button the user gets to choose executable file to which he wants to create a shortcut.

For example we have the game Doom 3 in the folder "c:\games\doom3\doom.exe"
He would then pick the executable, click on OK, then in the form click on "save", and a shortcut to that executable would appear in the folder "c:\shortcuts\"

I lurked around for a while and found a sample code in how to create a shortcut but I have no idea how I'm going to do it just like I explained above.

WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(@"D:\shotcut.lnk");
            shortcut.TargetPath = Application.ExecutablePath;
            //shortcut.IconLocation = 'Location of  iCon you want to set";
            // add Description of Short cut
            shortcut.Description = "Any Description here ";
            // save it / create

I also tried this code and it would give an exception on "shortcut.Save", is it because I'm using a 64 bit system?

Thanks in advance.
Posted 28-Jan-13 5:00am
Sergey Alexandrovich Kryukov 28-Jan-13 11:07am
What exception?
Steven Borges 28-Jan-13 11:10am
That sample creates a shortcut for the application, it's just a test, it doesn't point to any file, so I dunno why it doesn't work.
CHill60 28-Jan-13 11:14am
Have you built your test app ? As it stands you're trying to create a shortcut to the test application you're running. If the exe doesn't exist you'll get the exception you mention
Steven Borges 28-Jan-13 11:19am
Oh yeah that must be the reason, I haven't built it yet, that solves one of my questions.
Sergey Alexandrovich Kryukov 28-Jan-13 11:21am
OK, this is more then enough to find out the fix. File is not found — find it.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I haven't built it yet, that solves one of my questions

Following on from that I'm guessing the other question is how to cater for the user behaviour you described.
Add an openFileDialog to your form and then in your save button have something similar to
if (this.openFileDialog1.ShowDialog(this) != DialogResult.OK)
string f = openFileDialog1.FileName;
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(@"C:\shotcut.lnk");
shortcut.TargetPath = f;
shortcut.Description = "Any Description here ";

There are other ways (e.g. using methods on the openFileDiaglog itself) so this is not necessarily the best way but it should give you a starter
Steven Borges 29-Jan-13 8:30am
Gonna implement this now on my project, I'll give feedback in some time, thanks.
Steven Borges 29-Jan-13 10:33am
And it works, thank you very much !
CHill60 29-Jan-13 10:38am
My pleasure
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Hey Steve,
Try giving it a real absolute path (to a real file)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100