Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

Using VBScript in an installer package

4.68/5 (9 votes)
24 Sep 20052 min read 1   990  
How to use VBScript to enhance an installer package and guide the user through installation and configuration.

Introduction

An application often has some configuration that is system-specific. A connection string is an example of such a configurable setting. This setting must be adjusted by the user after installation, but before using the application.

The solution presented in this article helps to guide the installing user to change the configuration settings by simply opening this file in Notepad. It is merely a service above the installation manual.

Image 1

Background

In the recent past I have developed applications that needed configuration before use. For one project I made a really fancy installer class with a nice UI that asks for server and database names and that merged this info into the app.config.

The disadvantage of this approach is, that it is relatively hard to create such an installer class and even harder to maintain it. And the UI that you can create using a standard Visual Studio .NET setup and deployment project is very simple. For example, no input checking can be done.

My other option was just to instruct the installing user in an installation manual to install the program, then go to C:\Program Files\et cetera and edit the .config file that could be found there.

When developing and deploying a .NET Windows service I found that the last approach had too little service to the customer. The installing user had to adjust the CONFIG file and after that manually start the service. So I decided to search for a different solution, found none and created the one described here.

Using VBScript in the installer

My goal is really simple: open the .config file with Notepad automatically during installation. This can be done with a VBScript. To run a VBScript during installation, simply create a script, add it to your setup and deployment project and add it as a custom action, as shown in the demo code.

Notepad is opened with the .config file using this code:

VBScript
Dim curdir, WshShell

caption = "InstallerDemoService.Setup"
'Determine the path where this running script is. 
'That is also the path to the config-file.
'The path ("[TARGETDIR]") is being passed by the installer 
'in the one available parameter: CustomActionData
curdir = Session.Property("CustomActionData")

'Start Notepad with the config-file
Set WshShell = CreateObject ("WScript.Shell")
WshShell.Run ("notepad.exe """ + 
        curdir + "\InstallerDemoService.exe.config""")

Extra functionality

In the script that is run, anything can be done. In the demo code a question is asked whether or not to start the .NET Windows Service after installing. After answering 'Yes' the service will be started from the script.

Points of interest

In the VBScript the current directory is needed. Since a script within an installer package runs in its own scripting engine context, a vbs call to determine the path will not work (CreateObject("Scripting.FileSystemObject")). The solution is to pass the target directory in the single parameter that is available from the installer to the script:

VBScript
curdir = Session.Property("CustomActionData")

Conclusion

Using VBScript to enhance a setup and deployment project is easy to do and can be used for various tasks. I think it is an original way to extend an installer in this way.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here