I've been working with the Visual Studio deployment projects for years now, but always in a very simplistic manner. That is, I've used them to deploy my applications, install my services, etc, but I've never needed to get into custom development.
Sure, I've long leveraged .NET's ability to have Installer classes to execute custom code when the application in installed to perform tasks such as Event Log creation and/or registration. This is quite easy, in fact.
Without diving too deeply into it right now, a simple form comes down to the following tasks:
- Create a public class that derives from System.Configuration.Install.Installer.
- Add the [RunInstaller(true)] attribute to the class.
- Add the appropriate code to your Install, Rollback, and Commit method overrides.
- Add your application's output to your setup project's Install and Uninstall Custom Actions.
That's it - the installer takes care of everything.
However, my needs with a recent pet project involved passing data to my installer class from the setup project. This is easily accomplished by following these steps:
- Add a dialog to your installer (e.g. there's one called "Textboxes (A)" that will work fine).
- Each of these basic dialogs (A, B, and C) contains 4 edit fields. In order to pass the values from the edit fields to your installer you must name them. You can use the defaults, but EDITA1, EDITA2, etc don't tend to make much sense. You name them by changing the EditxProperty property on the dialog. This is now the variable name you can reference in your code.
- Pass the value to your installer by selecting the 'Primary output from XXX (Active)' node in the appropriate Custom Action and setting the CustomActionData property. This string is defined as a space delimited parameter list.
For instance, if you named a property 'ROOTFOLDER', you could define the parameter as follows:
/rootFolder=[ROOTFOLDER]
If you want to pass more than one parameter, you space delimit them:
/rootFolder=[ROOTFOLDER] /userName=[USERNAME]
Of course, if there's a space (or the potential for a space) in the value, you'll need to enclose the property in quotes:
/rootFolder="[ROOTFOLDER]"
- You can then reference the parameters by name in your installer code:
public override void Install(IDictionary stateSaver) {
base.Install(stateSaver);
try {
string rootFolder = this.Context.Parameters["rootFolder"];
// do something, such as create the folder, write a configuration file, etc
}
catch ( Exception ) {
// do some logging here
}
}
All in all it's pretty straightforward and easy to do.
Happy coding!