Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » Deployment » Poorman's Installation program in C# and .NET

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.

Page Views : 3490
Downloads : 45
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MGInstallCode.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mike Gold
Michael Gold is President of Microgold Software Inc., makers of the WithClass UML Tool. His company is a Microsoft VBA Partner and Borland Partner. He has a BSEE and MEng EE from Cornell University and has consulted for Chase Manhattan Bank, Merrill Lynch. Currently he is a senior consultant at JP Morgan Bank. He has been involved in several .NET book projects, and is currently working on a book for using .NET with embedded systems. He can be reached at mike@microgold.com
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.