Click here to Skip to main content
Sign Up to vote bad
good
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 - 5:00

Comments
Sergey Alexandrovich Kryukov - 28 Jan '13 - 11:07
What exception? —SA
Steven Borges - 28 Jan '13 - 11:10
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 - 28 Jan '13 - 11:14
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:19
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:21
OK, this is more then enough to find out the fix. File is not found — find it. —SA

2 solutions

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 - 29 Jan '13 - 8:30
Gonna implement this now on my project, I'll give feedback in some time, thanks.
Steven Borges - 29 Jan '13 - 10:33
And it works, thank you very much !
CHill60 - 29 Jan '13 - 10:38
My pleasure
Hey Steve,
Try giving it a real absolute path (to a real file)
 
Cheers,
Edo
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 355
1 Sergey Alexandrovich Kryukov 338
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,549
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid