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

时间:2014-07-29 20:52:00

标签: c# .net-4.0 shortcut

我正在构建自定义安装程序。如何在开始菜单中创建可执行文件的快捷方式?这是我到目前为止所提出的:

    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 个答案:

答案 0 :(得分:19)

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

答案 1 :(得分: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();
}

答案 2 :(得分:1)

这与此问题几乎相同:Create shortcut on desktop C#

要从answer复制,您需要自己创建快捷方式文件。

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

当然,这段代码未经测试,但在其他问题上被接受,看起来是正确的。

我在这个问题上看到了another answer,其中列出了如何使用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脚本宿主对象模型,"可以在"添加参考"那么" COM。"