Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Write to app.config in setup project and use it in the program

  • I have created a Installhelper.cs which is inhered from the Installer.
  • Override the Install() method by using this piece of code(A).

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

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

Any Help is greatly appreciated. I have 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 the app config is Code B:

<appSettings>
<add key="userName" value="" />
<add key="password" value="" />
<add key="foderPath" value="" />
</appSettings>
like image 757
diyoda_ Avatar asked Jun 28 '12 03:06

diyoda_


People also ask

How do I add a config section in app config?

We will start with creating an class that can store the instance settings (the <add> element), then we'll create a collection that can store our instances (the <instances> element) and then we'll create the class to manage the Configuration Section (the <sageCRM> element).

What is the use of app config in C#?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

The problem of this coding was this line

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

It does not give the path where the config file relies during the installation.So it has to be changed in to

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

    Configuration config = ConfigurationManager.OpenExeConfiguration(path);

Now its working. :)

like image 81
diyoda_ Avatar answered Nov 15 '22 11:11

diyoda_


Thank you. We had this exact issue and could not figure out why it would not write to the file. the only different thing I did was get the path from the Application.

string path = Application.ExecutablePath;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
like image 44
Jasmeet Avatar answered Nov 15 '22 09:11

Jasmeet