最近一个项目,需要在winform的安装过程中向app.config写入一些初始化的配置数据.
在安装过程中读取安装界面上的配置数据,参考这篇文章:http://www.cnblogs.com/soief/archive/2009/06/09/1499322.html
代码如下,其中 this.Context.Parameters["mastreip"]) 就是读取你安装界面上的数据.
[System.ComponentModel.RunInstaller(true)]
public class WriteConfigAction : System.Configuration.Install.Installer
{
public override void Install(System.Collections.IDictionary stateSaver)
{
try
{
MessageBox.Show(this.Context.Parameters["mastreip"]);
string masterIP = this.Context.Parameters["mastreip"];
Configuration config;
{
//读取config文件的安装路径,在你的安装目录中app.config会被重新命名为XXX.exe.config
config =
ConfigurationManager.OpenExeConfiguration(this.Context.Parameters["assemblypath"]);
}
if (config.AppSettings.Settings["MasterIP"] == null)
{
// Add an Application setting.
config.AppSettings.Settings.Add("MasterIP", masterIP);
}
else
{
config.AppSettings.Settings.Remove("MasterIP");
config.AppSettings.Settings.Add("MasterIP", masterIP);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
catch (Exception ex)
{
MessageBox.Show( ex.Message);
}
}
}
OK 完成,PS:在安装过程中这些代码无法跟进调试,耗费了我不少时间,有那位大虾可指点下如何跟进安装过程中写入的代码.