NotEqu's Blog

Installer class

  If you want to build your own setup project for any kind of application, it is good to know something about installer classes. I will make a little example to show you how to pass the value from custom setup dialogs to installer class.

  First you add installer class into your project or you can make new project just for installer classes inside your solution. I made widows form application and added installer1.cs into my application.

Slika1

Next, open design window for installer1.cs and in properties window on Events tab choose event you would like to override. In this case let's choose AfterInstall event.

Slika2

  You will see empty method that look's like this:

private void Installer1_AfterInstall(object sender, InstallEventArgs e)
{
}

  Add setup project to your solution. Because my installer class is in windows form project, all I have to do is open File System of my setup project and add primary output to Application Folder. Then open User Interface and add dialog you need. I added RadioButtons (3 buttons) and Textboxes (A) for my example. You can change order of dialogs just by dragging them up and down. This will be the order they will show during the installation.

Slika3

  Open properties window for RadioButtons dialog and enter text and values for radio buttons now:

Slika4

  Open properties window for Textboxes dialog. This dialog consists four text boxes and because I only need two, I will set visible property to true for the first two text boxes and false for the rest of them:

Slika5 

  Then open Custom Actions and add custom action you need. For this example we only need action OnInstall. Right click on Install folder and choose Add Custom Action. Then choose Application folder and select the primary output. You can rename it if you wish. I call it OnInstall.

Slika6

  Now we need to pass the values from custom dialogs to installer class. From the previous pictures you can see that RadioButtons dialog has property called ButtonProperty witch value is BUTTON3. BUTTTON3 represents the value we chosen with radio buttons. Open properties window for OnInstall action and for CustomActionData enter:

/TEST=[BUTTON3] /FNAME=[EDITA1] /LNAME=[EDITA2] /PATH="[TARGETDIR]\"

  We sad that BUTTON3 is the value of radio button we chose. EDITA1 and EDITA2 represents the value for text boxes. We have three parameters (TEST, FNAME and LNAME) which will we use to get to this values from installer class.

Slika7

  In Installer1_AfterInstall method we will now enter some code to do something with values from setup project:

 
private void Installer1_AfterInstall(object sender, InstallEventArgs e)
{
    string myPassedValue = this.Context.Parameters["TEST"];
    string fName = this.Context.Parameters["FNAME"];
    string lName = this.Context.Parameters["LNAME"];
    string path = this.Context.Parameters["PATH"];
    StreamWriter writer = new StreamWriter("C:\\Install.log", true);
    writer.WriteLine("Install log file");
    writer.WriteLine(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString());
    writer.WriteLine("Install path is " + path);
    string s = "";
    switch (myPassedValue)
    {
        case "1":
            s = "develop";
            break;
        case "2":
            s = "test";
            break;
        case "3":
            s = "product";
            break;
    }
    writer.WriteLine("You selected " + s);
    writer.WriteLine("First name: " + fName);
    writer.WriteLine("Last name: " + lName);
    writer.Flush();
    writer.Close();
}

 

  You can see that we used TEST, FNAME and LNAME parameters to get values from setup dialogs. All that this method will do is to put this values into install.log file. I hope you will find something more useful to do with installer classes then I in this example. Good luck!