I found a method to actually create the shortcut which utilizes the Windows Scripting Host object model. Most examples I found used a separate icon file for the shortcut, whereas I wanted to have the shortcut icon by default be the same one used by the executable itself.
First you must create a reference in Visual Studio to the Windows Script Host Object Model COM object as shown here:
Below is the modified method, where sLinkPathName is the path to the executable.
using IWshRuntimeLibrary ... private void appShortcutToDesktop(string sLinkPathName) { try { WshShellClass wsh = new WshShellClass(); IWshRuntimeLibrary.IWshShortcut scShortcut; // Choose the path for the shortcut scShortcut = wsh.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\shorcut.lnk") as IWshRuntimeLibrary.IWshShortcut; // Where the shortcut should point to scShortcut.TargetPath = txtFile.Text; // Description for the shortcut scShortcut.Description = "This is a shortcut."; // Location for the shortcut's icon scShortcut.IconLocation = sLinkPathName + ", 0"; // Create the shortcut at the given path scShortcut.Save(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
Line 22 sets the shortcut's IconLocation property such that it grabs the default icon for the executable, the same one that you'd expect to see if you created the shortcut manually.
3 comments:
It's very great post. This is really helpful for me.Thanks for sharing it.
worked really well! thanks!
www.flasharp.blogspot.com
The Shortcuts WinForms Control supports Ctrl, Alt and Shift with any other keys to trigger a specific action for their Windows applications.
Post a Comment