gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:18:01 27 4
gpt4 key购买 nike

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

    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。

最佳答案

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

关于c# - 如何创建开始菜单快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25024785/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com