Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / VBScript
Article

Using VBScript in an installer package

Rate me:
Please Sign up or sign in to vote.
4.68/5 (9 votes)
24 Sep 20052 min read 134.8K   984   23   14
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


Written By
Web Developer
Satellite Provider Satellite Provider
Since 1995 I have gathered wide experience in IT, in several different roles and in different environments. I am currently working for Capgemini Netherlands as Microsoft Software Developer, Software Architect and Project Manager.
In my leisure time I like to do sports, mainly speedskating and windsurfing. I also love to go cycling and to travel with my girlfriend and our son in camper.

Comments and Discussions

 
QuestionHow to run VB script before Installation Pin
Sumanta Rout13-Nov-07 4:11
Sumanta Rout13-Nov-07 4:11 
AnswerRe: How to run VB script before Installation Pin
Edwin Roetman13-Nov-07 9:03
Edwin Roetman13-Nov-07 9:03 
QuestionWhy WSH? Pin
Richard Deeming29-Sep-05 4:26
mveRichard Deeming29-Sep-05 4:26 
AnswerRe: Why WSH? Pin
Edwin Roetman29-Sep-05 9:28
Edwin Roetman29-Sep-05 9:28 
GeneralRe: Why WSH? Pin
Richard Deeming29-Sep-05 22:49
mveRichard Deeming29-Sep-05 22:49 
GeneralRe: Why WSH? Pin
Edwin Roetman30-Sep-05 9:19
Edwin Roetman30-Sep-05 9:19 
GeneralRe: Why WSH? Pin
Casp9-May-07 2:32
Casp9-May-07 2:32 
GeneralRe: Why WSH? Pin
Richard Deeming9-May-07 5:29
mveRichard Deeming9-May-07 5:29 
GeneralTo Registry Pin
qumer10128-Sep-05 1:31
qumer10128-Sep-05 1:31 
QuestionRe: To Registry Pin
Edwin Roetman28-Sep-05 11:06
Edwin Roetman28-Sep-05 11:06 
AnswerRe: To Registry Pin
qumer10129-Sep-05 0:44
qumer10129-Sep-05 0:44 
GeneralRe: To Registry Pin
Edwin Roetman29-Sep-05 9:39
Edwin Roetman29-Sep-05 9:39 
GeneralRe: To Registry Pin
qumer10129-Sep-05 20:23
qumer10129-Sep-05 20:23 
GeneralRe: To Registry Pin
_____11-Feb-09 7:18
_____11-Feb-09 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.