I am building a custom installer. How can I create a shortcut to an executable in the start menu? This is what I\'ve come up with so far:
string pathToExe
This is pretty much the same as this question: Create shortcut on desktop C#.
To copy from that answer, you'll need to create the shortcut file yourself.
using (StreamWriter writer = new StreamWriter(appStartMenuPath + ".url"))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine("URL=file:///" + pathToExe);
writer.WriteLine("IconIndex=0");
string icon = pathToExe.Replace('\\', '/');
writer.WriteLine("IconFile=" + icon);
}
This code is untested, of course, but it was accepted on that other question and it looks right.
I see another answer on that question that lists how to do it with the Windows API and some COM interop, but I'd be tempted to shy away from that and just use the above code if it works. It's more personal preference than anything, and I'd normally be all for using a pre-established API for this, but when the solution is this simple I'm just not sure how worth it that option really is. But for good measure, I believe this code should work. Again, of course, it's totally untested. And especially here where you're playing with things like this, make sure you understand each line before you execute it. I'd hate to see you mess up something on your system because of a blind following to code I posted.
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(appStartMenuPath + ".lnk");
shortcut.Description = "Shortcut for TestApp";
shortcut.TargetPath = pathToExe;
shortcut.Save();
You will, of course, also need a reference to the "Windows Script Host Object Model," which can be found under "Add a Reference" then "COM."
Using the Windows Script Host (make sure to add a reference to the Windows Script Host Object Model, under References > COM tab):
using IWshRuntimeLibrary;
private static void AddShortcut()
{
string pathToExe = @"C:\Program Files (x86)\TestApp\TestApp.exe";
string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp");
if (!Directory.Exists(appStartMenuPath))
Directory.CreateDirectory(appStartMenuPath);
string shortcutLocation = Path.Combine(appStartMenuPath, "Shortcut to Test App" + ".lnk");
WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);
shortcut.Description = "Test App Description";
//shortcut.IconLocation = @"C:\Program Files (x86)\TestApp\TestApp.ico"; //uncomment to set the icon of the shortcut
shortcut.TargetPath = pathToExe;
shortcut.Save();
}
By MSI setup, you can easily create Start Menu Shortcut for your application. But when it comes to custom installer setup, you need to write custom code to create All Programs shortcut. In C#, you can create shortcut using Windows Script Host library.
Note: To use Windows Script Host library, you need to add a reference under References > COM tab > Windows Script Host Object Model.
See this article for more info: http://www.morgantechspace.com/2015/01/create-start-menu-shortcut-all-programs-csharp.html
Create shortcut only for Current User:
string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
string shortcutFolder = Path.Combine(programs_path, @"YourAppFolder\SampleApp");
if (!Directory.Exists(shortcutFolder))
{
Directory.CreateDirectory(shortcutFolder);
}
WshShellClass shellClass = new WshShellClass();
string settingsLink = Path.Combine(shortcutFolder, "Settings.lnk");
IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
shortcut.TargetPath = @"C:\Program Files\YourAppFolder\MyAppSettings.exe";
shortcut.IconLocation = @"C:\Program Files\YourAppFolder\settings.ico";
shortcut.Arguments = "arg1 arg2";
shortcut.Description = "Click to edit SampleApp settings";
shortcut.Save();
Create Shortcut for All Users:
You can get common profile path for All Users by using API function SHGetSpecialFolderPath.
using IWshRuntimeLibrary;
using System.Runtime.InteropServices;
---------------------------------
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_COMMON_STARTMENU = 0x16;
public static void CreateShortcutForAllUsers()
{
StringBuilder allUserProfile = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, allUserProfile, CSIDL_COMMON_STARTMENU, false);
//The above API call returns: C:\ProgramData\Microsoft\Windows\Start Menu
string programs_path = Path.Combine(allUserProfile.ToString(), "Programs");
string shortcutFolder = Path.Combine(programs_path, @"YourAppFolder\SampleApp");
if (!Directory.Exists(shortcutFolder))
{
Directory.CreateDirectory(shortcutFolder);
}
WshShellClass shellClass = new WshShellClass();
//Create Shortcut for Application Settings
string settingsLink = Path.Combine(shortcutFolder, "Settings.lnk");
IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
shortcut.TargetPath = @"C:\Program Files\YourAppFolder\MyAppSettings.exe";
shortcut.IconLocation = @"C:\Program Files\YourAppFolder\settings.ico";
shortcut.Arguments = "arg1 arg2";
shortcut.Description = "Click to edit SampleApp settings";
shortcut.Save();
}