0

我进行了很多搜索,但仍然找不到正确的解决方案。

我想使用 .net 设置向导创建设置,并在设置向导中添加 2 个复选框。

第一个复选框将要求用户“在启动时添加程序”。如果选中,则会将软件添加到启动中。

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

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

4

1 回答 1

1

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

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

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

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

于 2012-04-19T14:02:21.757 回答