Poorman's Installation program in C# and .NET


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.  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, an 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 1 - The Installation program in action

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 the 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:

Listing 1 - XML File containing Installation Information

<?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>

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:

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

public void FillStringArray(string[] a, ref int num, string nodename)
{
MoveNextNode();
// move to the next element in the list of nodes
MoveNextNode();
int count = 0;
string test = 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++;
// keep a count of the number of elements in the array
MoveNextNode();
MoveNextNode();
}
num = count;
// set it equal to the number of array elements to return through the reference variable, num
}

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.

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

void GetDataForRepository()
{
bool endscript = false;
while (endscript == false)
{
repository.MoveNextNode();
string nextElement = repository.GetNextElementType();
switch (nextElement)
{
case "gradient":
repository.FillAttributes(
ref InstallData.GradientColor1, ref InstallData.GradientColor2);
break;
case "welcomegif":InstallData.WelcomeGIF = repository.GetNextElementString();
break;
case "title":InstallData.Title = repository.GetNextElementString();
break;
case "product":InstallData.Product = repository.GetNextElementString();
break;
case "company":InstallData.Company = repository.GetNextElementString();
break;
case "application":repository.FillStringArray(InstallData.ApplicationFiles, ref InstallData.NumApplicationFiles, "file");
break;
case "system":repository.FillStringArray(InstallData.SystemFiles, ref InstallData.NumSystemFiles, "file");
break;
case "billboards":repository.FillStringArray(InstallData.Billboards, ref InstallData.NumBillboardFiles, "billboard");
break;
case "readme":InstallData.ReadMeFile = repository.GetNextElementString();
break;
case "EndScript":endscript = true;
break;
}
}
}

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:

Listing 4 - Creating a shortcut in the start menu

private IWshRuntimeLibrary.IWshShell_Class TheShell = new IWshRuntimeLibrary.IWshShell_Class();
private void OKButton_Click(object sender, System.EventArgs e)
{
// Use the Environment class to retrieve the Start Menu on the system
string startMenu = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
// Use the name of the company to create the Start Menu folder for your application
string DirectoryPath = startMenu + "\\" + ScreenForm.InstallData.Company;
if (Directory.Exists(DirectoryPath) == false)
Directory.CreateDirectory(DirectoryPath);
// Loop through the selected files and create shortcuts for them in the Start Menu\Company folder
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
{
IWshRuntimeLibrary.IWshShortcut_Class shellLink = (IWshRuntimeLibrary.IWshShortcut_Class)TheShell.CreateShortcut(DirectoryPath + "\\" + listBox1.SelectedItems[i] + ".lnk");
shellLink.TargetPath = DirectoryPath + "\\" + listBox1.SelectedItems[i];
shellLink.Description = listBox1.SelectedItems[i].ToString();
shellLink.IconLocation = listBox1.SelectedItems[i] + ", 0";
shellLink.Save();
}
this.Hide();
ScreenForm.EndForm.Show();
}

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 need 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 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. 


Similar Articles