Monday, June 11, 2012

How to Create Setup file in windows application?



Create windows application

First we need to create one windows application. Then we create set up file for that application.
Select File -> New -> Project in Visual Studio Standard tool bar.
image1


Give Name for your windows application
image2


Design side
I have design my form with one text box and one DataGridView look like this
image3


In this data grid view I have bind data from MS Access Database. And I use that text box for search records based on name.

Server Side
I have declare variables and connection details in the class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.Diagnostics;

namespace WindowsSetup
{
    public partial class Form1 : Form
    {
        //You can place your database "Data-directory/Databasefile" under your project bin/Debug folder
        static string s=Application.StartupPath + "\\Data\\test.mdb";
        static string constr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + ";";
        
        OleDbConnection con=new OleDbConnection(constr);
        OleDbCommand cmd=new OleDbCommand();
        OleDbDataAdapter da=new OleDbDataAdapter();
        DataTable dt=new DataTable();
 

If you want uninstall option for your set up file then write code to get Command Line argument like below.
public Form1()
{
    InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
   //Below code is used to uninstall application
   
   //Get Command Line Arguments
   string[] arguments = Environment.GetCommandLineArgs();            
   foreach (string argument in arguments)
   {
       if (argument.Split('=')[0].ToLower() == "/u")
       {
           string guid = argument.Split('=')[1];
           string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
           ProcessStartInfo si = new ProcessStartInfo(path + "\\msiexec.exe", "/i " + guid);
           Process.Start(si);
           Close();
           Application.Exit();
           System.Environment.Exit(0);
       }
   }   

   //Load DataGridView first time when form load
   refreshGrid();        
}

void refreshGrid()
{
    string query;
    con.Open();
    if(textBox1.Text=="")
    {
        query="select eno,empname,sal from emp";
    }
    else
    {
        query = "Select eno,empname,sal from emp where empname like '%" + textBox1.Text + "%'";
    }
    cmd=new OleDbCommand(query,con);
    da = new OleDbDataAdapter(cmd);
    dt.Clear();
    da.Fill(dt);
    dataGridView1.DataSource = dt;
    con.Close();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    //Refresh grid values when each text entered in textbox by user.
    refreshGrid();
}           
}

That's all we done create one windows application. Compile this application and run it. Output look like below.

output


How to create setup file for .Net Application?

Now we can create set up file for this application with MS Access database.
Go to File - > Add-> New project in the Existing opened Project Visual Studio screen.
image4


Then Select "Setup and Deployment" and right side select "Set up Project" (under visual studio installed templates) in the dialogue box. Give the name for your set up file.
image5


After enter name click Ok button. Screen look like below. Check it that set up file added in your solution explorer (right side)
image6


Now right click on the application folder select Add->Project Output.
image7


After select New pop up appear in that window select "Primary Ouput" Click Ok.
image8


How add MS Access database to the Set up file?

I have placed access database in my application "Data" directory so I need to create directory for same like this in application folder. Right Click of the application folder and choose Add - > New folder and name it for that folder "Data"
image9


Double click that "Data" folder in the application folder to open, now right click and choose Add->File
image10


After select new dialogue box is appear and ask for your database location. Select your database path and click ok in that dialogue box. After added it check your database attached in Application folder look like this.
image11


How to add Desktop Shortcut in setup file?

Select Application folder in File System and right click of the Primary Output file to create short cut for that file.
image12


Rename of that shortcut and cut that shortcut file
image13


After cut that shortcut go to File System (left side) user's desktop, double click that folder and open it and paste that shortcut
image14


If you want change the icon of short cut then select that short cut right click -> choose properties window. In that property window choose icon option to set icon for your desktop shortcut. Make sure if you want add icon in the short cut then you must add that icon in the application folder before.

How to create Shortcut in Programs menu during set up creation?

Select Application folder in File System and right click of the Primary Output file to create short cut
image12


Rename that shortcut same like above steps and Create one new folder in user's Program menu and paste that shortcut under that folder.
image15


How to create uninstall Shortcut in Programs menu?

Select Application folder in File System and right click of the Primary Output file to create short cut
image12


Rename that shortcut to "uninstall" same like above steps and Paste that shortcut under user's programs menu like this
image16


Select that uninstall short cut right click choose Properties.
image17


In that property window give Arguments value /u=[ProductCode].
image18


Now select set up file in solution explorer and build it
image19


In this example I have create project "D:\WindowsSetup" name. So now I go to my project location"D:\WindowsSetup" Set up file folder "Windows_Setup" is available in that project folder and set up file is available in this path "D:\WindowsSetup\Windows_Setup\Debug" folder.
image20


Just I double click that set up file to install in my system. After installation Shortcut for that application is created automatically in Desktop and Program files menu.


Desktop shortcut
image21


Program Menu Shortcut with uninstall option
image22

No comments:

Post a Comment