Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
3.50/5 (4 votes)
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.
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.

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
Share this answer
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
Hey Steve,
Try giving it a real absolute path (to a real file)

Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900