Get user input during installation using setup pro

2020-04-28 11:14发布

问题:

I have created an installer to install a windows service. Installer should ask user to name the service. Hence, I have created a custom UI in the installer with TextBox.

When user runs the setup.exe file, installation starts and the custom UI shows up. User adds the name for the windows service but how shall I take the ServiceName as user input during installation and set default service name into service name provided by user during installation in that custom UI.

回答1:

You need to follow these steps:

  1. Create a Windows Service project. It will create a service project containing Service1.cs.
  2. Open Service1.cs and right click and choose Add Installer. It will create a ProjectInstaller.cs.
  3. Add a new Setup Project. (If you don't have project template, download and install it from here for VS2013, VS2015 or VS2017)
  4. Add primary output of the service project to the setup project.
  5. Go to User Interface window and a new Dialog using TextBoxes (A) template after Installation Folder. (Set visible property of other textboxes than Edit1 to false.)
  6. Go to Custom Actions window and add a new custom action from primary output of the service project.
  7. In the Custom Action window, go to this node go to this node Custom ActionsInstallPrimary outputfrom WindowsService1 and in Properties window, set the value of CustomActionData to /SVCNAME=[EDITA1].
  8. Open ProjectInstaller.cs in your service project and override Install to set Name or DisplayName of the service:

    public override void Install(IDictionary stateSaver)
    {
        string value = Context.Parameters["SVCNAME"];
        this.serviceInstaller1.DisplayName = value;
        base.Install(stateSaver);
    }
    

Then build your projects and install the service.