惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 小呆也行

数据库分区(一) SQL SERVER 分区 向大数据进军 SqlServer中查看数据库所有表的表空间和索引空间信息 SQL 遍历父子关系表(二叉树)获得所有子节点 所有父节点(转) url问题 - 小呆也行 - 博客园 COM+的使用(转) Sql中查找数据库中,所有包含字段的表名 死亡机器(转) 厚黑口才学大全(读后感) ASP TO ASP.NET migration,a new approach (Reprinted) ASP向ASP.AET迁移要注意的问题(转) Linq的模糊查询 - 小呆也行 - 博客园 0.4-0.3==0.1 听懂面试官问题背后的潜台词 - 小呆也行 聪明人把知道说出来,而智者则不声张 我一进教室就震惊了 注册assembly的问题 学习.NET 事例网站
C#winform部署中自定义配置文件
小呆也行 · 2012-06-05 · via 博客园 - 小呆也行

Configure App.config Application Settings During MSI Install

written by Rob Aquila on Wednesday, September 03 2008

I have been recently working on a setup project in Visual Studio 2008 to deploy a windows application and wanted the ability for the user to enter in values during the install that would then be saved in the app.config file.  After some playing around with and some research I figured out a way to do this. 

For this example I have a solution with two projects.  One project is the main application and the other is a setup project.  There are 3 variables that I will be capturing from the user and storing in the app.config file.  Here is what the app configuration settings look like in the app.config file.

<?xml version="1.0encoding="utf-8" ?> 

<configuration

  <appSettings 

    <add key="Param1value="" />  

    <add key="Param2value="" />  

    <add key="Param3value="" />  

  </appSettings 

</configuration> 

To update these 3 settings during the install process the first thing you would need to do if you haven't already is to create a setup project in Visual studio. 

image

Once the setup project is created you will need to add the main application's output by right clicking the setup project -> Add -> Project Output.  Then select primary output from the main project.

You will then need to create the dialog box that will be shown during the install process to collect the user's settings.  To do this right click the setup project -> View -> User Interface as shown below.

image

Once you see the list of dialogs as shown above, right click Start -> Add Dialog -> Text Boxes (A) to add the new dialog.  Once the dialog is created select it in the list and then navigate to that dialog's properties page.  The dialog that I have chosen has the ability to show 4 fields.  Since I only need to collect 3 parameters, I'm going to hide the 4th field.  As you can see below I set each field's label and set Edit4Visible to false.

image

The result is a dialog box that looks like this during the install process.

image

Now that you have a screen that will collect the variables from the user you will need a way to capture the values and save them into the app.config file.  This is where creating an installer class comes into play.  The installer gives you the hook you need to implement your custom code during the install process.  Add an installer class to the main application by right clicking the project -> Add -> New Item, choose Installer class.

image

After you create a new installer class in the main application, override the Install method.  Below is the code I created for this sample.  As you can see I collect all the user entered values from the context object.  I will explain how these values get passed into the context object in the next section.  I also capture the targetdir so that I can locate the app.config file.  Finally, I open the config file and update the application settings to the new values and save.  If you need to debug and step through your installer code you have to call Debugger.Break, since normal break points do not work while running an install.

[RunInstaller(true)]

public partial class MyInstaller : Installer 

{

      public MyInstaller()

      {

            InitializeComponent(); 

      }

      public override void Install(System.Collections.IDictionary stateSaver)

      {

            base.Install(stateSaver);

            string targetDirectory = Context.Parameters["targetdir"];

            string param1 = Context.Parameters["Param1"];

            string param2 = Context.Parameters["Param2"];

            string param3 = Context.Parameters["Param3"];  

            //System.Diagnostics.Debugger.Break();

            string exePath = string.Format("{0}MyWindowsFormsApplication.exe", targetDirectory);

            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            config.AppSettings.Settings["Param1"].Value = param1;

            config.AppSettings.Settings["Param2"].Value = param2;

            config.AppSettings.Settings["Param3"].Value = param3;

            config.Save();

      }

} 

The final piece is to add a custom action so that the above installer code gets called.  To do this right click the setup project -> View -> Custom Actions.  You should then see a folder called install as shown below.  Right click that folder and select add custom action.  You should then see a dialog box, navigate to the Application Folder and select primary output from the main application.  Once that is completed navigate to the properties screen for the primary output that was added.  It should look something like the image below.

image

The final step is to add the CustomActionData values.  This string determines what gets passed into the context object that we used above to collect the user values that were entered.  As you can see, I'm storing each text box value into a param value.  I also pass in the targetdir.  Once this is complete, rebuild the solution and you now have an installer that can update the app.config file with the desired user values.  This is also useful when you want to have one version of the app.config file in your project that you use locally to debug with, and a deployed version with defaulted values set in the installer.

CustomActionData format style is :

  • 对于作为安装组件的自定义操作(ProjectInstaller 类),“CustomActionData”属性采用 /name=value 形式。其中的每个名称都必须是唯一的,并且仅有一个值。多个值之间必须用一个空格隔开:/name1=value1 /name2=value2。如果值本身有一个空格,则必须在该值两侧加上引号:/name="a value"。

    使用加括号的语法:/name=[PROPERTYNAME],可以传递 Windows Installer 属性。对于像“[TARGETDIR]”这样返回目录的 Windows Installer 属性,除了加括号外,还必须加引号和尾部反斜杠:/name="[TARGETDIR]\"。

    原文地址:http://raquila.com/software/configure-app-config-application-settings-during-msi-install/