How to write app.config in an installation project and use it in a program

  • I created Installhelper.cs, which is built in from the installer.
  • Override the Install () method using this piece of code (A).

These values ​​inserted in app.config are not available in the [projectName] .exe.config file that is created after installation. I already added a section as shown below in app.config manually (B)

data is passed to the installer class, but the data is not written to the app.config fields. They remain the same in the created configuration file during installation.

Any help is appreciated. I spent almost a day on this.

Code A:

[RunInstaller(true)] public partial class Installation : System.Configuration.Install.Installer { public Installation() { InitializeComponent(); } public override void Install(IDictionary stateSaver) { //base.Install(stateSaver); try { // In order to get the value from the textBox named 'EDITA1' I needed to add the line: // '/PathValue = [EDITA1]' to the CustomActionData property of the CustomAction We added. string userName = Context.Parameters["userName"]; string password = Context.Parameters["password"]; string folderPath = Context.Parameters["path"]; MessageBox.Show(userName); MessageBox.Show(password); MessageBox.Show(folderPath); Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //config.AppSettings.Settings.Add("userName", userName); //config.AppSettings.Settings.Add("password", password); //config.AppSettings.Settings.Add("foderPath", folderPath); config.AppSettings.Settings["userName"].Value = userName; config.AppSettings.Settings["password"].Value = password; config.AppSettings.Settings["foderPath"].Value = folderPath; config.Save(ConfigurationSaveMode.Full); ConfigurationManager.RefreshSection("appSettings"); } catch (FormatException e) { string s = e.Message; throw e; } } } 

Added section in application configuration Code B:

 <appSettings> <add key="userName" value="" /> <add key="password" value="" /> <add key="foderPath" value="" /> </appSettings> 
+7
source share
2 answers

Thanks. We had this exact problem, and we could not understand why it would not write to the file. the only thing I did was get the path from the application.

 string path = Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(path); 
+1
source

The problem with this encoding was this line

 Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

It does not give the path in which the configuration file is used during installation. Therefore, it must be changed to

 string path = Path.Combine(new DirectoryInfo(Context.Parameters["assemblypath"].ToString()).Parent.FullName, "[project name].exe") Configuration config = ConfigurationManager.OpenExeConfiguration(path); 

Now its work. :)

+6
source

All Articles