c#创建快捷方式并添加到开始菜单程序目录
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
c#创建快捷⽅式并添加到开始菜单程序⽬录
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(monStartMenu);//or Environment.GetFolderPath(Environment.SpecialFolder.Programs); string appStartMenuPath = bine(commonStartMenuPath, "Programs", "TestApp");
if (!Directory.Exists(appStartMenuPath))
Directory.CreateDirectory(appStartMenuPath);
string shortcutLocation = bine(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:
public static void CreateStartMenuShortcut()
{
string programs_path = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
string shortcutFolder = bine(programs_path, @"MorganTechSpaceSampleApp");
if (!Directory.Exists(shortcutFolder))
{
Directory.CreateDirectory(shortcutFolder);
}
WshShellClass shellClass = new WshShellClass();
//Create First Shortcut for Application Settings
string settingsLink = bine(shortcutFolder, "Settings.lnk");
IWshShortcut shortcut = (IWshShortcut)shellClass.CreateShortcut(settingsLink);
shortcut.TargetPath = @"C:\Program FilesMorganTechSpaceMyAppSettings.exe";
shortcut.IconLocation = @"C:\Program FilesMorganTechSpacesettings.ico";
shortcut.Arguments = "arg1 arg2";
shortcut.Description = "Click to edit MorganApp settings";
shortcut.Save();
//Create Second Shortcut for Uninstall Setup
string uninstallLink = bine(shortcutFolder, "Uninstall.lnk");
shortcut = (IWshShortcut)shellClass.CreateShortcut(uninstallLink);
shortcut.TargetPath = @"C:\Program FilesMorganTechSpaceSetup.exe";
shortcut.IconLocation = @"C:\Program FilesMorganTechSpaceuninstall.ico";
shortcut.Arguments = "u";
shortcut.Save();
}。