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!

Comments

NotEqu said:

Silly me :-)

On OnInstall action for CustomActionData property add:

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

and in Installer1_AfterInstall() function add line:

string path = this.Context.Parameters["PATH"];

# kolovoz 6, 2008 4:26

anonymous said:

nice :)

# kolovoz 21, 2008 7:48

anonymous said:

I tried the above code but is is throwing exception on path line.

"Exception occure while initializing the installation:

System.IO.FileNotFoundException: Could not load files or assembly"

# kolovoz 22, 2008 12:17

NotEqu said:

Sorry again :-)

Windows Installer properties can be passed using the bracketed syntax: /name=[PROPERTYNAME]. For Windows Installer properties such as [TARGETDIR] that return a directory, in addition to the brackets you must include quotation marks and a trailing backslash: /name="[TARGETDIR]\". So try with:

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

# kolovoz 28, 2008 3:45

anonymous said:

Hi,

I want from the user's decision in the previous dialog to control if the next dialog will show.

How can I do it?

Thanks,

Hadar.

# rujan 11, 2008 12:45

anonymous said:

Hi,

I want to know if there is an option to control if a dialog will be shown from the user's decision in the previous dialog.

Thanks,

Hadar.

# rujan 11, 2008 12:48

anonymous said:

cool..

I was looking for the same stuff.

Thanks..

# rujan 22, 2008 12:04

anonymous said:

Excellent article.

How I can set value in EDITA1 as value of current logged in user. I can get the current logged in user using below statement.

string sLoggeInUser = WindowsIdentity.GetCurrent().Name.ToString();

But How I can assign the value to EDITA1.

# listopad 5, 2008 7:49
Leave a Comment

(required) 

(required) 

(optional)

(required)