How to Create Setup file for .NET windows application?

朴灿烈づ我的快乐病毒、 2022-01-06 20:19 363阅读 0赞

In this article I have explained about how to create set up file with MS Access Database, Uninstall option, etc. Each steps I have clearly explained in this article with screen shot. First step (create windows application) to last step Uninstall option all are covered in this article.

**Description

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.

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.OleDb;
  10. using System.Configuration;
  11. using System.Diagnostics;
  12. namespace WindowsSetup
  13. {
  14. public partial class Form1 : Form
  15. {
  16. //You can place your database "Data-directory/Databasefile" under your project bin/Debug folder
  17. static string s=Application.StartupPath + "\\Data\\test.mdb";
  18. static string constr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + s + ";";
  19. OleDbConnection con=new OleDbConnection(constr);
  20. OleDbCommand cmd=new OleDbCommand();
  21. OleDbDataAdapter da=new OleDbDataAdapter();
  22. DataTable dt=new DataTable();

If you want uninstall option for your set up file then write code to get Command Line argument like below.

  1. public Form1()
  2. {
  3. InitializeComponent();
  4. }
  5. private void Form1_Load(object sender, EventArgs e)
  6. {
  7. //Below code is used to uninstall application
  8. //Get Command Line Arguments
  9. string[] arguments = Environment.GetCommandLineArgs();
  10. foreach (string argument in arguments)
  11. {
  12. if (argument.Split('=')[0].ToLower() == "/u")
  13. {
  14. string guid = argument.Split('=')[1];
  15. string path = Environment.GetFolderPath(Environment.SpecialFolder.System);
  16. ProcessStartInfo si = new ProcessStartInfo(path + "\\msiexec.exe", "/i " + guid);
  17. Process.Start(si);
  18. Close();
  19. Application.Exit();
  20. System.Environment.Exit(0);
  21. }
  22. }
  23. //Load DataGridView first time when form load
  24. refreshGrid();
  25. }
  26. void refreshGrid()
  27. {
  28. string query;
  29. con.Open();
  30. if(textBox1.Text=="")
  31. {
  32. query="select eno,empname,sal from emp";
  33. }
  34. else
  35. {
  36. query = "Select eno,empname,sal from emp where empname like '%" + textBox1.Text + "%'";
  37. }
  38. cmd=new OleDbCommand(query,con);
  39. da = new OleDbDataAdapter(cmd);
  40. dt.Clear();
  41. da.Fill(dt);
  42. dataGridView1.DataSource = dt;
  43. con.Close();
  44. }
  45. private void textBox1_TextChanged(object sender, EventArgs e)
  46. {
  47. //Refresh grid values when each text entered in textbox by user.
  48. refreshGrid();
  49. }
  50. }

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

转载于:https://www.cnblogs.com/Jenny90/p/3339870.html

发表评论

表情:
评论列表 (有 0 条评论,363人围观)

还没有评论,来说两句吧...

相关阅读