How to create start menu shortcut

泄露秘密 提交于 2019-12-31 21:09:39

问题


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 = @"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'm targeting Windows 7.


回答1:


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(); 
}



回答2:


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();
}



回答3:


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."



来源:https://stackoverflow.com/questions/25024785/how-to-create-start-menu-shortcut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!