Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
            shortcut.Save();
 
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 6:00am
Comments
Sergey Alexandrovich Kryukov at 28-Jan-13 11:07am
   
What exception?
—SA
Steven Borges at 28-Jan-13 11:10am
   
FileNotFound.
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 at 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 at 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 at 28-Jan-13 11:21am
   
OK, this is more then enough to find out the fix. File is not found — find it.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Quote:
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)
	return;
string f = openFileDialog1.FileName;
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(@"C:\shotcut.lnk");
shortcut.TargetPath = f;
shortcut.Description = "Any Description here ";
shortcut.Save();
 
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
  Permalink  
Comments
Steven Borges at 29-Jan-13 8:30am
   
Gonna implement this now on my project, I'll give feedback in some time, thanks.
Steven Borges at 29-Jan-13 10:33am
   
And it works, thank you very much !
CHill60 at 29-Jan-13 10:38am
   
My pleasure
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hey Steve,
Try giving it a real absolute path (to a real file)
 
Cheers,
Edo
  Permalink  
v2

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

  Print Answers RSS
0 DamithSL 320
1 OriginalGriff 155
2 Peter Leow 115
3 Afzaal Ahmad Zeeshan 114
4 deepakdynamite 110
0 OriginalGriff 7,510
1 DamithSL 5,519
2 Sergey Alexandrovich Kryukov 4,994
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
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