This solution uses a Setup Project within Visual Studio 2008 and makes use of the builtin Installer
class found in the .NET framework. Other links can be found here and here.
Here's a basic outline of what to do:
- Add an
Installer
class instance to the project that will be installed to the client's PC
- Add a setup project that will install the application to the client's PC
- Define actions within the setup project that will be raised at certain points during the installation
- Override relevant methods on the
Installer
class to get database connection details from the end user during application installation
- Preserve the database connection settings (in case of an uninstall)
- Run your custom method/class/executable within the context of the installer and do what must be done
Here's a step-by-step guide:
- Add
Installer
class: an Installer
class must be housed inside the application to be installed. In Visual Studio's Solution Explorer, select the application to be installed and click the menu items "Project->Add New Item->Installer Class". This will add a default installer class called "Installer1".
- In Visual Studio's Solution Explorer, select the Setup project and right click it. Select the "View->Custom actions" to display the custom actions to be executed during installation.
- In the "Custom actions" window, add custom actions for "Install" and "Uninstall"
- Go to the
Installer
class ("Installer1") and override OnBeforeInstall
.
Here's pseudocode of how things would work:
[Installer1.cs]
protected override void OnBeforeInstall(IDictionary savedState)
{
base.OnBeforeInstall(savedState);
MessageBox.Show("OnBeforeInstall: " + GetProperties(savedState));
using (ConnectionDialog d = new ConnectionDialog())
{
d.ShowDialog();
savedState["database"] = d.Database;
savedState["user"] = d.User;
savedState["password"] = d.Password;
savedState["integrated"] = d.Integrated;
}
}
The IDictionary
instance passed in by the installer is a collection of key/value pairs which users can populate with their relevant data. This same information is passed into other methods such as OnBeforeUninstall
which the developer can use to detach the database or rollback changes or whatever else.