如何创建开始菜单快捷方式

15

我正在构建一个自定义安装程序。如何在开始菜单中创建指向可执行文件的快捷方式?这是我目前的想法:

    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

我针对的是Windows 7操作系统。

3个回答

31

使用Windows脚本宿主(确保在“引用”>“COM”选项卡下添加对Windows脚本宿主对象模型的引用):

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
通过MSI安装程序,您可以轻松地为应用程序创建开始菜单快捷方式。但是,在使用自定义安装程序时,您需要编写自定义代码来创建所有程序的快捷方式。在C#中,您可以使用Windows脚本主机库创建快捷方式。
注意:要使用Windows脚本主机库,您需要在“引用> COM选项卡> Windows脚本主机对象模型”下添加引用。
有关更多信息,请参见此文章:http://www.morgantechspace.com/2015/01/create-start-menu-shortcut-all-programs-csharp.html 仅为当前用户创建快捷方式:
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();

为所有用户创建快捷方式:

您可以使用API函数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();
}

1
这与在C#中创建桌面快捷方式的问题几乎相同。
要从答案中复制,您需要自己创建快捷方式文件。
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);
}

当然,这段代码没有经过测试,但它在另一个问题上被接受,并且看起来是正确的。

我在那个问题上看到另一个答案列出了如何使用Windows API和一些COM互操作来完成它,但如果上面的代码有效,我会倾向于回避它。这更多是个人喜好,通常我会支持使用预先建立的API来完成此任务,但当解决方案如此简单时,我不确定这个选项真正值得多少。但为了保险起见,我相信这段代码应该可以工作。同样,当你在处理这种东西时,确保你理解每一行代码再执行它。我不想看到你因为盲目跟随我发布的代码而弄坏了系统。

WshShell shell = new WshShell();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(appStartMenuPath + ".lnk");
shortcut.Description = "Shortcut for TestApp";
shortcut.TargetPath = pathToExe;
shortcut.Save();

当然,您还需要引用“Windows Script Host对象模型”,可以在“添加引用”中找到,然后选择“COM”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接