在系统托盘中创建通知图标,并使用 .NET 安装程序创建向导添加启动快捷方式

本文关键字:创建 程序 安装 NET 向导 快捷方式 启动 添加 系统 通知 图标 | 更新日期: 2023-09-27 18:31:50

我已经搜索了很多,但仍然无法找到正确的解决方案。

我想使用 .net 安装向导创建安装程序,并在安装向导中添加 2 复选框。

第一个复选框将要求用户"在启动时添加程序"。如果选中,则它将添加软件以启动。

第二个复选框将要求"创建系统托盘通知图标"。如果选中,它将在系统托盘中创建通知图标。通知图标不仅在应用程序运行时必须永久显示)。

我知道这与自定义操作有关,但仍然无法弄清楚。请为我提供一些文章或代码,并提供适当的解释。

在系统托盘中创建通知图标,并使用 .NET 安装程序创建向导添加启动快捷方式

实际上,自定义操作是执行此操作的正确位置。 这是我在安装过程中用于创建两个自定义复选框的代码。

[RunInstaller(true)]
    public class DeploymentManager : Installer{

  public override void Install(IDictionary stateSaver) {
     base.Install (stateSaver);

const string DESKTOP_SHORTCUT_PARAM = "DESKTOP_SHORTCUT";
const string QUICKLAUNCH_SHORTCUT_PARAM = "QUICKLAUNCH_SHORTCUT";
const string ALLUSERS_PARAM = "ALLUSERS";
        // The installer will pass the ALLUSERS, DESKTOP_SHORTCUT and QUICKLAUNCH_SHORTCUT   
        // parameters. These have been set to the values of radio buttons and checkboxes from the
        // MSI user interface.
        // ALLUSERS is set according to whether the user chooses to install for all users (="1") 
        // or just for themselves (="").
        // If the user checked the checkbox to install one of the shortcuts, then the corresponding 
        // parameter value is "1".  If the user did not check the checkbox to install one of the 
        // desktop shortcut, then the corresponding parameter value is an empty string.

        bool allusers = true; // Context.Parameters[ALLUSERS_PARAM] != string.Empty;
  bool installDesktopShortcut = true; //Context.Parameters[DESKTOP_SHORTCUT_PARAM] != string.Empty;
        bool installQuickLaunchShortcut = true;// Context.Parameters[QUICKLAUNCH_SHORTCUT_PARAM] != string.Empty;
if (installDesktopShortcut){
    // If this is an All Users install then we need to install the desktop shortcut for 
   // all users.  .Net does not give us access to the All Users Desktop special folder,
   // but we can get this using the Windows Scripting Host.
   string desktopFolder = null;
         if (allusers){
       try{
    // This is in a Try block in case AllUsersDesktop is not supported
    object allUsersDesktop = "AllUsersDesktop";
    WshShell shell = new WshShellClass();
         desktopFolder = shell.SpecialFolders.Item(ref allUsersDesktop).ToString();
}
catch {}
  }
if (desktopFolder == null)
desktopFolder = Environment.GetFolderPathEnvironment.SpecialFolder.DesktopDirectory);
CreateShortcut(desktopFolder, ShortcutName, Path.Combine(TargetAssemblyFolder, TargetAssembly), ShortcutDescription, Path.Combine(TargetAssemblyFolder, "your.ico"));
        }
        if (installQuickLaunchShortcut){
            CreateShortcut(QuickLaunchFolder, ShortcutName, ShortcutFullName, ShortcutDescription, Path.Combine(TargetAssemblyFolder, "your.ico"));
        }
    }
private void CreateShortcut(string folder, string name, string target, string description, string targetIcon){
string shortcutFullName = Path.Combine(folder, name + ".lnk");
try{
    WshShell shell = new WshShellClass();
    IWshShortcut link = (IWshShortcut)shell.CreateShortcut(shortcutFullName);
    link.TargetPath = target;
    link.Description = description;
    FileInfo fi = new FileInfo(targetIcon);
    link.IconLocation = Path.Combine(fi.Directory.FullName, fi.Name);
    link.Save();
}catch (Exception ex){
    MessageBox.Show(string.Format("The shortcut '"{0}'" could not be created.'n'n{1}", shortcutFullName, ex.ToString()),
    "Create Shortcut", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

获得此代码后,可以将自定义操作添加到安装程序的"安装自定义操作"区域。

安装过程的通知代码类似,但需要添加到注册表中。