visual-studio 如何制作 System.Configuration.Install.Installer 以从安装项目中获取变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2066048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a System.Configuration.Install.Installer to get a variable from the Setup project?
提问by Jader Dias
I have 2 projects in my solution
我的解决方案中有 2 个项目
A Windows service
Its Setup project
Windows 服务
它的安装项目
I need that my ProjectInstaller : System.Configuration.Install.Installer's method called OnAfterInstall to get the ProductNamefrom the Setup Project. How do I do that?
我需要我ProjectInstaller : System.Configuration.Install.Installer的方法调用 OnAfterInstallProductName从安装项目中获取。我怎么做?
回答by Somedeveloper
Within your setup project right click project and select View > Custom Actions. Add a custom action. Now select Add Output, select your web service project, and click OK.
在您的设置项目中,右键单击项目并选择“查看”>“自定义操作”。添加自定义操作。现在选择“添加输出”,选择您的 Web 服务项目,然后单击“确定”。
Now select your custom action and set the CustomActionDataproperty to contain something like /ProductName=[PRODUCTNAME] /whateveryouwant=[Whateveryouwant](note that these are key-value pairs; i.e. to access the product name, ProductNameis the key and the value is PRODUCTNAME).
现在选择您的自定义操作并将CustomActionData属性设置为包含类似的内容/ProductName=[PRODUCTNAME] /whateveryouwant=[Whateveryouwant](请注意,这些是键值对;即访问产品名称,ProductName是键值是PRODUCTNAME)。
Note that CustomActionDatacontains the parameters that will be passed to your installer class. The PRODUCTNAMEis the property name associated with the input control in the user interface dialog, and so in your case you would prompt user for Product Name within yor installer. So the label is "Product Name" and the corresponding property should be set as PRODUCTNAME(obviously you could change this, but the most important thing to note is that the UI property name must be the same as the property name in the CustomActionData) for this example to work.
请注意,它CustomActionData包含将传递给安装程序类的参数。这PRODUCTNAME是与用户界面对话框中的输入控件关联的属性名称,因此在您的情况下,您将在您的安装程序中提示用户输入产品名称。所以标签是“产品名称”,对应的属性应该设置为PRODUCTNAME(显然你可以改变这个,但最重要的是要注意UI属性名称必须与CustomActionData本示例中的属性名称相同)去工作。
Now within your installer class you can get product name by doing
现在在您的安装程序类中,您可以通过执行以下操作来获取产品名称
public override void Install(IDictionary stateSaver)
{
// If you need to debug this installer class, uncomment the line below
//System.Diagnostics.Debugger.Break();
string productName = Context.Parameters["ProductName"].Trim();
string whateveryouwant = Context.Parameters["whateveryouwant"].Trim();
}
note i included the commented code //System.Diagnostics.Debugger.Break();which you can comment in so that you can debug the installer class.
请注意,我包含了注释代码//System.Diagnostics.Debugger.Break();,您可以在其中进行注释,以便您可以调试安装程序类。
hope this helps.
希望这可以帮助。

