I’ve been trying and failing for quite some time at creating my configuration based on values enterend through the installer interfaces… And today I finally succeeded.

Let me walk you through the hardships and how to get it to work.

Create a console application that displays a name from the configuration file. (pretty easy so far)

class Program

{

    static void Main(string[] args)

    {

        var config = MyConsoleApplicationSection.Load();

 

        Console.Out.WriteLine(“Name = {0}”, config.Name);

 

        Console.ReadLine();

    }

}

 

class MyConsoleApplicationSection : ConfigurationSection

{

    [ConfigurationProperty(“Name”, IsRequired = true)]

    public string Name

    {

        get { return this[“Name”].ToString(); }

        set { this[“Name”] = value; }

    }

 

    public MyConsoleApplicationSection(string name)

    {

        this.Name = name;

    }

 

    public MyConsoleApplicationSection() { }

 

    public static MyConsoleApplicationSection Load()

    {

        return (MyConsoleApplicationSection)ConfigurationManager.GetSection(“MyConsoleApplicationSection”);

    }

}

And now for the harder part, I want to create an installer and in this installer I want to set the name in a textbox.

Let us start by creating a new project and select a setup and deployment project | setup project.

I’ll name mine NameSetup and click on OK.  Next we add a Project output to the application folder, and we choose our program’s active configuration. Now do a right click on the Setup Project and select View | User Interface

View | User Interface

View | User Interface

Right click Install | Start in the treeview and Add Dialog | TextBoxes (A). You’ll see that we have to move textboxes (A) up 2 positions in order to be able to build the project so lets do that.

Now we change some of the properties on the TextBoxes (A) dialog.

Properties

Properties

Ok. The click work is almost finished…

Now click on the setup project again in your solution explorer, and select the View | Custom Actions

Rigth click on the Custom Actions Node (main) | Add Custom Action …

Select the application folder and choose the output.

On properties of the custom action that is added under the install folder we need to change the CustomActionData propertie value to ” /name=[EDITA1] ” (without quotes)

The EDITA1 value refers to the first textbox on the dialog we’ve just added. You can add other values in the same manner (like this /name=[EDITA1] /type=[EDITA2] /something=[EDITA3] … ‘catch my drift’)

Now our setup project is ready. We only have to do one more thing. Add a new component to the project you want to setup, the installer class. Add following code to the installer class:

  public override void Install(IDictionary stateSaver)

  {

      base.Install(stateSaver);

 

      var assemblyPath = Context.Parameters[“assemblyPath”];

      var name = Context.Parameters[“name”];

      var configuration = new MyConsoleApplicationSection(name);

      var conf = ConfigurationManager.OpenExeConfiguration(assemblyPath);

 

      conf.Sections.Add(“MyConsoleApplicationSection”, configuration);

      conf.Save(ConfigurationSaveMode.Modified);

  }

One more thing. Because there seems to be a bug or something in the OpenExeConfiguration, we can not read the MyConsoleApplicationSection in the installer class. I haven’t found a solution to this for now, but there is a workaround. Just remove the configuration section from your app.Config entirely (configsection part and MyConsoleApplicationSection). Now when we call the save method (like on the last line above) the file is saved correctly.

 

So thats about it for this post. If you have any remarks or improvements, or this has been a great help please let me know.

See y’all next time…

Advertisements