使用安装项目在安装过程中获取用户输入并在安装程序中使用它 [英] Get user input during installation using setup project and use it in installer
问题描述
我创建了一个安装程序来安装 Windows 服务.安装程序应要求用户为服务命名.因此,我在安装程序中使用 TextBox
创建了一个自定义 UI.
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
.
当用户运行 setup.exe 文件时,安装开始并显示自定义 UI.用户添加了 Windows 服务的名称,但是我应该如何在安装过程中将 ServiceName
作为用户输入并将默认服务名称设置为用户在安装过程中在该自定义 UI 中提供的服务名称.
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.
推荐答案
您需要按照以下步骤操作:
You need to follow these steps:
- 创建一个 Windows 服务项目.它将创建一个包含
Service1.cs
的服务项目. - 打开
Service1.cs
并右键单击并选择添加安装程序.它将创建一个ProjectInstaller.cs
. - 添加一个新的设置项目.(如果您没有项目模板,请从此处下载并安装 VS2013、VS2015 或 VS2017)
- 将服务项目的主要输出添加到设置项目.
- 转到用户界面窗口,并在安装文件夹之后使用TextBoxes (A)模板创建一个新对话框.(将除
Edit1
之外的其他文本框的可见属性设置为false
.) - 转到自定义操作窗口并从服务项目的主要输出添加一个新的自定义操作.
- 在自定义操作窗口中,转到此节点转到此节点自定义操作→安装→WindowsService1的主要输出 并在 Properties 窗口中,将
CustomActionData
的值设置为/SVCNAME=
. 在您的服务项目中打开
ProjectInstaller.cs
并覆盖Install
以设置Name
或DisplayName
服务:
- Create a Windows Service project. It will create a service project containing
Service1.cs
. - Open
Service1.cs
and right click and choose Add Installer. It will create aProjectInstaller.cs
. - Add a new Setup Project. (If you don't have project template, download and install it from here for VS2013, VS2015 or VS2017)
- Add primary output of the service project to the setup project.
- Go to User Interface window and a new Dialog using TextBoxes (A) template after Installation Folder. (Set visible property of other textboxes than
Edit1
tofalse
.) - Go to Custom Actions window and add a new custom action from primary output of the service project.
- In the Custom Action window, go to this node go to this node Custom Actions → Install → Primary outputfrom WindowsService1 and in Properties window, set the value of
CustomActionData
to/SVCNAME=
. Open
ProjectInstaller.cs
in your service project and overrideInstall
to setName
orDisplayName
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.
这篇关于使用安装项目在安装过程中获取用户输入并在安装程序中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!