Custom Installation using SetUp and Deployment Project

Niladri.Biswas
Posted by in Windows Forms category on for Beginner level | Points: 250 | Views : 6903 red flag

In this article, we will look into the Custom Installation using SetUp and Deployment Project.


 Download source code for Custom Installation using SetUp and Deployment Project

Introduction

Setup and deployment wizard has been a friend for developers for a long time.For creation of the basic deployment wizard, we donot need any customization of our setup program.However, if there is a need to customize the setup operation like introducng some screens in-between the normal process and capture the values or creation of some files at runtime once the delpoyment is over needs some special attenetion and for that we need to perform advanced customization of teh setup. This article will focus on that.

Purpose

We want the following process to be in place

  • We will introduce a two screens before the installation process.
  • Screen1:This screen will be named as "Database Server Detail".There will be two textboxes - one will capture "Database Server Title", the second one will capture "Database Server Address".

  • Screen2:This screen will be named as "Database Detail".There will be three textboxes - one will capture "Database Name", the second one will capture "User Name" and teh third one will capture "Password".

  • Then once the installation is over, it will generate a xml file say "DatabaseServers.xml" whose structure will be

    	<?xml version="1.0" encoding="utf-8"?>
    	<DataBaseServers>
    	  <DataBaseServer id="1" title="MyServerInstance" address="MyServerAddress" />
    	  <DataBases>
    		<Database id="1" name="MyDatabase" userid="testuser" password="testpassword" />
    	  </DataBases>
    	</DataBaseServers>
    	

Step Involved for this

Step1:Create a blank solution file.

Step2:Add a "WindowsForm" Application to the solution and develop a screen as under

Add the code in the "Load" button click event

private void btnLoad_Click(object sender, EventArgs e)
{
	string path = Path.Combine(Environment.CurrentDirectory, "DataSource\\DatabaseServers.xml");
	XDocument xdoc = XDocument.Load(path);
	XElement dbServers = xdoc.Element("DataBaseServers");
	
	var dbServerDetailResult =  (from dbServer in dbServers.Elements("DataBaseServer")
			select new 
			{
				ServerId = Convert.ToInt32(dbServer.Attribute("id").Value),
				Name = dbServer.Attribute("title").Value,
				Address = dbServer.Attribute("address").Value                       
			}).ToList()[0];

	var dbDetailResult = (from dbDetail in dbServers.Elements("DataBases")
				   from x in dbDetail.Elements("Database")
				   select new 
				   {
					   DbId = Convert.ToInt32(x.Attribute("id").Value),
					   DbName = x.Attribute("name").Value,
					   UserName = x.Attribute("userid").Value,
					   PassWord = x.Attribute("password").Value
				   }).ToList()[0];

	txtTitle.Text = dbServerDetailResult.Name;
	txtAddress.Text = dbServerDetailResult.Address;
	txtDBName.Text = dbDetailResult.DbName;
	txtUserName.Text = dbDetailResult.UserName;
	txtPassword.Text = dbDetailResult.PassWord;
}

Step3:Add a "Setup and deployment Project" to the solution.Right click on the "Application Folder"->Add->Project Output.Choose "Windows Form Application".Click "OK".

At this stage, it will look as

Next, click on the "View"->"UserInterface"

And we will get the below screen.

Right click on "Start"->"Add Dialog"

And the "Add Dialog" screen will appear as under

From there choose , "TextBoxes(A)" and click "OK".In a similar fashion, choose "TextBoxes(B)".

Right Click on the "TextBoxes(A)" and choose "MoveUp"

Proceed the same for "TextBoxes(B)".Finally it will appear as

Next, right click on the "TextBoxes(A)" and from the properties window, do the below

Make sure that "Edit3Visible" and "Edit4Visible" will be set to "False" because we want to display only the first two textboxes for this window

Next, right click on the "TextBoxes(B)" and from the properties window, do the below

Make sure that "Edit4Visible" will be set to "False" because we want to display only the first three textboxes for this window

Step4:Add a class library project(say "CustomAction") to our solution.Add a reference to the "System.Confirguration.Install" assembly.

Add a class(say "MycustomAction") that inherits from the "Installer" class.

The "Installer" class provides the foundation for custom installations.

Add the attribute [RunInstaller(true)]. The new class should override the four main methods: Install, Uninstall, Commit, and Rollback.

Step5:Right click on the "CustomSetUp_Deployment" and "View"->"Custom Actions".

It will appear as

Right Click on "CustomAction"->"Add Custom Action".

The "Select Item in Project" will appear

Double click on the "Application folder" and click on "Add Output" button

From the "Add Project Output Group", add the "CustomAction" library project.Click "OK"

Right click on the "Primary output from CustomAction (Active)" under the install folder and click on Properties Window.

In the "CustomActionData" write

/DBServerTitle="[EDITA1]" /DBServerAddress="[EDITA2]" /DBName="[EDITB1]" /UserName="[EDITB2]" /Password="[EDITB3]" /targetdir="[TARGETDIR]\"

Custom Action Data will accept all input parameters as an single string

Step6:Visit the "MycustomAction.cs" file and write the below in the "Install" method

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
public override void Install(IDictionary savedState)
{
	    base.Install(savedState);

            string installationPath = this.Context.Parameters["targetdir"]; 
            string databaseServerTitle = Context.Parameters["DBServerTitle"];
            string databaseServerAddress = Context.Parameters["DBServerAddress"];
            string dbName = Context.Parameters["DBName"];
            string userName = Context.Parameters["UserName"];
            string password = Context.Parameters["Password"];

            XDocument doc =
                         new XDocument(
                           new XElement("DataBaseServers",
                             new XElement("DataBaseServer", new XAttribute("id", "1"), new XAttribute("title", databaseServerTitle), new XAttribute("address", databaseServerAddress)),
                             new XElement("DataBases",
                                 new XElement("Database", new XAttribute("id", "1"), new XAttribute("name", dbName), new XAttribute("userid", userName), new XAttribute("password", password))
                             )));

            var currentDirectory = Path.Combine(installationPath, "DataSource");
            bool IsExists = System.IO.Directory.Exists(currentDirectory);
            if (!IsExists) System.IO.Directory.CreateDirectory(currentDirectory);
           
            var fileName = Path.Combine(currentDirectory, "DatabaseServers.xml");
            doc.Save(fileName); 
}

Verification

Build the SetUp project.Then Install it.

The custom action will be executed.Once installed, open the exe file and click on the "Load" button.The output will be as under

References

How to create a custom action using Csharp

A Setup and Deployment project that passes parameters to the configuration file

Conclusion

Hope this will be helpful for those who want's to build Custom Installation using SetUp and Deployment Project.Thanks for reading.Zipped file is attached

Page copy protected against web site content infringement by Copyscape

About the Author

Niladri.Biswas
Full Name: Niladri Biswas
Member Level: Platinum
Member Status: Member
Member Since: 10/25/2010 11:04:24 AM
Country: India
Best Regards, Niladri Biswas
http://www.dotnetfunda.com
Technical Lead at HCL Technologies

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)