Sample code: Implementing a custom installer for .NET 2.0

August 13, 2008

Implementing a custom installer

This is an example class, which would need to be created in the same assembly as the application needing to be installed:

[System.ComponentModel.RunInstaller(true)]
class NewInstaller : Installer
{
    public NewInstaller() : base()
    {
        this.Committing += new InstallEventHandler
            (NewInstaller_Committing);
        this.Committed += new InstallEventHandler
            (NewInstaller_Committed);
    }

    private void NewInstaller_Committing(object sender,
        InstallEventArgs e)
    {
        // Whatever needs to be done before installation
    }
    private void NewInstaller_Committed(object sender,
        InstallEventArgs e)
    {
        // Whatever needs to be done after installation
    }
    public override void Install(IDictionary savedState)
    {
        base.Install(savedState);
    }
    public override void Commit(IDictionary savedState)
    {
        base.Commit(savedState);
    }
    public override void Rollback(IDictionary savedState)
    {
        base.Rollback(savedState);
    }
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
    }
}

Launching a custom installer programmatically

static void Main(string[] args)
{
    IDictionary actions = new Hashtable();
    try
    {
        AssemblyInstaller aInstaller = new AssemblyInstaller
            ("ExampleInstaller.dll", args);
            // or you could use the ComponentInstaller
            // object instead
        aInstaller.UseNewContext = true;
        aInstaller.Install(actions);
        aInstaller.Commit(actions);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Uninstalling or Rolling Back an Installation

Replace the Install() method from the previous example with one of the following methods:

aInstaller.Uninstall(actions);

aInstaller.Rollback(actions);

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.