How to create a Start Menu shortcut

I am creating a custom installer. How to create a shortcut for an executable file in the Start menu? This is what I came up with:

string pathToExe = @"C:\Program Files (x86)\TestApp\TestApp.exe"; string commonStartMenuPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu); string appStartMenuPath = Path.Combine(commonStartMenuPath, "Programs", "TestApp"); // TODO: create shortcut in appStartMenuPath 

I am targeting Windows 7.

+8
c # shortcut
source share
3 answers

Using the Windows Script host (be sure to add a link to the Windows Script host object model in the Links> 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(); } 
+16
source share

Using MSI, you can easily create a Start Menu Shortcut for your application. But when it comes to customizing the user installer, you need to write your own code to create the โ€œAll Programsโ€ shortcut. In C #, you can create a shortcut using the Windows Script Host library .

Note. To use the Windows Script Host Library , you need to add a link in the Links> COM tab> Windows Script Host Object Model .

See this article for more information: 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 a shortcut for all users :

You can get a common profile path for all users using the SHGetSpecialFolderPath API function .

 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(); } 
+2
source share

This is almost the same as this question: Create a shortcut on the C # desktop .

To copy this answer , you need to create a 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 has not been verified, of course, but it has been adopted on this other issue and looks correct.

I see another answer on this question that lists how to do this using the Windows API and some COM interaction, but I will be tempted to avoid this and just use the code above if it works. This is a more personal preference than anything, and I usually had to use a pre-installed API for this, but when the solution is so simple, I'm just not sure how much it really costs. But for a good measure, I believe this code should work. Again, of course, this is completely untested. And especially here, where you play with such things, make sure you understand each line before you execute it. I would really like you to ruin something in your system due to blindly following the code that 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 link to the Windows Object Model Windows Script, which can be found in the Add Link section, then COM.

+1
source share

All Articles