ARTICLE

Poorman's Installation Program in VB.NET

Posted by Mike Gold Articles | Visual Basic 2010 June 06, 2003
Installation programs can be quite expensive these days. Although programs such as Wise and InstallShield have fantastic features and are fairly comprehensive, sometimes you just want to do the easy installation of copying and pasting files into a directory and perhaps placing a shortcut in the start menu.
Download Files:
 
Reader Level:

Installation programs can be quite expensive these days.  Although programs such as Wise and Install Shield have fantastic features and are fairly comprehensive, sometimes you just want to do the easy installation of copying and pasting files into a directory and perhaps placing a shortcut in the start menu.  This simple program can do just that without breaking the bank.  It also contains some of the flashy features of Wise and InstallShield like a progress bar, timed billboards, and an XML file to describe your "installation script". Best of all it comes with source code so you can alter it to suit your needs.  Below is the UML Design for the Installation Program.


Figure 2 - Installation Program UML Design reverse engineered using WithClass 2000

The program works by launching each successive form after pressing the Next button in the present form. The forms each have different functions.  Form1 is the welcome form. It is the first step in guiding you through the installation.  The next form is the InstallFolderForm.  This form allows you to select the destination folder for your application.  Choosing next vutton in this form brings up the ProgressForm. This for shows the ProgressBar with the files that are being copied to the destination folder.  It also is responsible for bringing up the graphic billboards.  The StartMenuDialog brings up a form to prompt you as to which files you wish to place in the start menu.  Finally FinalDialog is brought up which allows you to choose a readme file and exit the installation.

There are two interesting aspects of this application I would like to discuss in this article.  One it use of an XML File as a repository.  The other is how to use the Windows Shell COM interface and Environment class to create shortcuts in the start menu.

The "installation script" consists of a well-formed XML file with information about the product you are installing, the company, the files, the billboards, and even the gradient colors of the background screen.

Below is the XML file used for the WithClass Installation sample:

<?xml version="1.0" encoding="utf-8" ?>
<
Document>
<gradient color1="Blue" color2="LightBlue" />
<welcomegif>logo.gif</welcomegif>
<strings>
<title>WithClass 2000 UML Tool</title>
<company>Microgold Software Inc.</company>
<product>WithClass</product>
<version>8.0</version>
</strings>
<files>
<application>
<file>wc2000.exe</file>
<file>wc2000.dll</file>
</application>
<system>
<file>mfc42.dll</file>
</system>
</files>
<billboards>
<billboard>billboard1.bmp</billboard>
</billboards>
<readme>readme.txt</readme>
<EndScript></EndScript>
</
Document>

Listing 1 - XML File containing Installation Information.

The XmlDatabase class is used to parse the information from this file.  It uses an XmlTextReader to read the different elements of the script.  In a sense, the XMLDatabase class is a wrapper around the XmlTextReader class to enhance the specific node reading functionality needed for this particular application.  For example, one of the methods in the XmlDatabase, FillStringArray, allows you to read a list of XML elements into a string array.  This is required for reading the list of files to copy or for reading the list of billboards to display.  The code for this method is shown below:

Public Sub FillStringArray(ByVal a() As String, ByRef num As Integer, ByVal nodename As String)
MoveNextNode()
' move to the next element in the list of nodes
MoveNextNode()
Dim count As Integer = 0
Dim test As String = GetNextElementType() ' get the type of node
' loop while the type of node equals the type we are interested in for our array
While GetNextElementType() = nodename
' get the contents of the next node
a(count) = GetNextElementString()
count += 1
' keep a count of the number of elements in the array
MoveNextNode()
MoveNextNode()
End While
num = count ' set it equal to the number of array elements to return through the reference variable, num
End Sub 'FillStringArray

Listing 2 - Filling a string array from a list of similar xml elements.

The XmlDatabase class reads the xml file and fills a structure with the xml contents called the InstallData structure.  This is all done in the GetDataForRepository method in the MainForm class.

Sub GetDataForRepository()
Dim endscript As Boolean = False
While endscript = False
repository.MoveNextNode()
Dim nextElement As String = repository.GetNextElementType()
Select Case nextElement
Case "gradient"
repository.FillAttributes(InstallData.GradientColor1, InstallData.GradientColor2)
Case "welcomegif"
InstallData.WelcomeGIF = repository.GetNextElementString()
Case "title"
InstallData.Title = repository.GetNextElementString()
Case "product"
InstallData.Product = repository.GetNextElementString()
Case "company"
InstallData.Company = repository.GetNextElementString()
Case "application"
repository.FillStringArray(InstallData.ApplicationFiles, InstallData.NumApplicationFiles, "file")
Case "system"
repository.FillStringArray(InstallData.SystemFiles, InstallData.NumSystemFiles, "file")

Case
"billboards"
repository.FillStringArray(InstallData.Billboards, InstallData.NumBillboardFiles, "billboard")
Case "readme"
InstallData.ReadMeFile = repository.GetNextElementString()
Case "EndScript"
endscript =
True
End Select
End While
End
Sub 'GetDataForRepository

Listing 3 - Retrieving data from the Xml File into the InstallData structure.

Once we have the data from the XML script, we can use it throughout our installation program.

The next topic of discussion is how we populate the start menu with a shortcut.  Admittedly, I did not know how to do this when I started, but found some obscure thing about it in MSDN.  The first thing I did was right clicked on the solution explorer, chose Add Reference, and searched for a COM reference to the Windows Shell Object.  After scrolling around, I managed to find it.  By selecting the reference, it automatically creates a wrapper class around the Dll so you can access the Windows Shell COM interface as a class.  Below is the code that was used to populate the start menu and create a shortcut link:

Private TheShell As New IWshRuntimeLibrary.IWshShell_Class
Private Sub OKButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Use the Environment class to retrieve the Start Menu on the system
Dim startMenu As String = Environment.GetFolderPath
Environment.SpecialFolder.StartMenu)
' Use the name of the company to create the Start Menu folder for your application
Dim DirectoryPath As String = startMenu + "\" + ScreenForm.InstallData.Company
If Directory.Exists(DirectoryPath) = False Then
Directory.CreateDirectory(DirectoryPath)
End If
' Loop through the selected files and create shortcuts for them in the Start Menu\Company folder
Dim i As Integer
For i = 0 To listBox1.SelectedItems.Count - 1
Dim shellLink As IWshRuntimeLibrary.IWshShortcut_Class = CType
TheShell.CreateShortcut((DirectoryPath + "\" + listBox1.SelectedItems(i) + ".lnk")), IWshRuntimeLibrary.IWshShortcut_Class)
shellLink.TargetPath = DirectoryPath + "\" + listBox1.SelectedItems(i)shellLink.Description = listBox1.SelectedItems(i).ToString()
shellLink.IconLocation = listBox1.SelectedItems(i) + ", 0"
shellLink.Save()
Next i
Me.Hide()
ScreenForm.EndForm.Show()
End Sub 'OKButton_Click

Listing 4 - Creating a shortcut in the start menu.

Note the Environment class is used to find out where the directory of the Start Menu is located.  You can also use the Environment class to get other special folder locations.  Another useful SpecialFolder enumeration is the SpecialFolder.DesktopDirectory.  You can use this enumeration in the code above to create a shortcut to your application right on the desktop.

Improvements

Having played around a lot with InstallShield and Wise, I could think of many options you could add to the installation program to make it more powerful.  It would be nice to have a section in the xml file to list ocx's and dll files that needs to be both copied and registered. It would be great if you could cancel the install during the copying stage.  It would be nice to have a feature that allowed you to launch any program after the installation was complete.  Probably its not a bad idea to add a license file dialog and a dialog that prompts for the serial number.  Also a registration dialog that emails registration to the company would be a cool addition.

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor