How C # WinForm program is packaged as a Setup project (diagram)

Source: Internet
Author: User
Tags visual studio installer

http://blog.csdn.net/u011981242/article/details/51059441

Development environment: Vs2010+sql Server 2008

Operating system: Win7_32bit flagship edition

Development language: C #

Project name: Student Homestay Management System


Let's start with: How do I package Windows applications?

Open VS2010, open the project you want to package, then right-click on "Solution", "Add", "New project" and pop up the interface as shown:

Click on the "Install and deploy" triangle to the left, select "Visual Studio Installer" below, select "Setup Project" and change the name below to "Setup" to click OK.



Click "Setup" generated in the solution to change the protectname in the attribute to "Student Homestay system V1.0" (Your project name)



Right-click "Setup" in the Solution and select "Properties". The Popup property page interface follows the second picture:

Then click on the system prerequisites inside.

Important: Tick the "Download prerequisites from the same location as my application (D)", which means that when you tick, when you build the Setup project, the components that you check in the prerequisites list are available under the path of your installation project. (The system automatically completes, this is good, do not need you to download components)

1), Windows Installer 3.1 (required)

2),. NET Framework 3.5 (optional) Reference last description

3), Crystal Reports Basic for Visual Studio2008 (x86,x64) (optional) in the project, you need to tick this.



Right-click on "Application Folder", click "Add", then click on the folder, named "DB" (optional) for storing your database files.

Then right-click on the "DB" folder you just added, "Add", "File" and add your database.




Right click on "Application Folder", click "Add", click "File". Add all of the files in your release directory below.



Right click on "Application Folder", click "Add", select "Project Output ...", note: In the Project column to select your own project (my Project name: studentjisu), then select "Main Output", click OK.




Create a desktop shortcut, right-click the main output you just added, then select the first create shortcut, and then you can rename the shortcut (I renamed it: Student Homestay management System)

Finally, click the left mouse button to place the shortcut, and then drag and drop it under the User Desktop folder.




Create an uninstall program. Right click on "Application Folder", click "Add", select "File", and then "C:\Windows\System32" under the "Msiexec.exe" file to add, if not found, you can search directly. Of course, you can also create a shortcut for Msiexec.exe named "UnInstall".



After naming the shortcut, copy the Setup property (click the Setup pop-up property in the solution) ProductCode to the arguments of the Uninstall property:

At the same time, add "/X" to the front, note that there is a space behind X.



Change the logo of the desktop shortcut. Your own logo is too bad, you can download a picture on the Internet, and then convert to. ico format.

In the "Application Folder" refers to the location of the logo storage, usually exist in that place.



Attach the database. We will now add a class for writing additional database code.

Right-click on "Solution", click "Add", select "New Project", then create a new C # class library and name it "Installdb".

Finally, delete the words "Class1.cs".




Create a new class for writing the code that the database attaches to the database management system. Right-click the newly created "Installdb", click "Add" and select "New Item".

Then, in the popup screen, select "Installer Class" and name "InstallDB.cs".




Because the attached database requires users to enter some information about the native database, such as: server name, database administrator name and password, and so on. At this point, we can pop up a box waiting for user input during the installation process:

Right click on "Setup", click "View" and select "User Interface". Pop up the second screen below, then right click on "Start", click "Add Dialog", select "text box (A)", and drag it to "welcome" below the third picture below.

Finally, fill in the "text box (A)" attribute according to your own needs, you can refer to the third picture.

Note: The variables defined inside are defined primarily for the additional code below.





Add the primary output of the attached database. Right-click Setup, select View, Custom action.

Then, right-click on "Install", select "Application Folder", select the Installer Class "Installdb", or select "main output" to determine.

Next, copy and paste the following in the Costomactiondata:

 
        <span style=

=[]/pwd=[pwd]/targetdir=</span>





Write the additional database code in InstallDB.cs. Click "Stand here to switch to Code view" first.

Then add several namespaces.

Of course, to use the MessageBox () function, you need to add a using System.Windows.Forms; You also need to add a System.Windows.Forms reference (action: Right-click Installdb, select Add Reference, select. NET)


Of course, the last written code is as follows:

[CSharp]View PlainCopy
  1. Using System;
  2. Using System.Collections;
  3. Using System.Collections.Generic;
  4. Using System.ComponentModel;
  5. Using System.Configuration.Install;
  6. Using System.Linq;
  7. Using System.Data.SqlClient;
  8. Using System.Windows.Forms;
  9. Using System.IO;
  10. Using System.Security.AccessControl;
  11. Namespace Installdb
  12. {
  13. [Runinstaller (true)]
  14. Public partial class InstallDB:System.Configuration.Install.Installer
  15. {
  16. Public installdb ()
  17. {
  18. InitializeComponent ();
  19. }
  20. //Create a database
  21. private void CreateDatabase (string strSQL, string dataname, string strmdf, string strldf, String path)
  22. {
  23. SqlConnection myconn = new SqlConnection (strSQL);
  24. String str = null;
  25. Try
  26. {
  27. str = @"EXEC sp_attach_db @dbname = '" + dataname + "', @filename1 = '" + strmdf + "', @filename2 = '" + strldf + "'";
  28. SqlCommand mycommand = new SqlCommand (str, myconn);
  29. MyConn.Open ();
  30. Mycommand.executenonquery ();
  31. MessageBox.Show ("Database installation succeeded! Click OK to continue "); //need using System.Windows.Forms
  32. }
  33. catch (Exception e)
  34. {
  35. MessageBox.Show ("Database installation failed!    "+ e.message + " \ n "+" you can manually append data ");
  36. System.Diagnostics.Process.Start (path); //Open the installation directory
  37. }
  38. finally
  39. {
  40. Myconn.close ();
  41. }
  42. }
  43. //Rights Management
  44. private static void Setfullcontrol (string path)
  45. {
  46. FileInfo info = new FileInfo (path);
  47. FileSecurity fs = info. GetAccessControl ();
  48. Fs. Addaccessrule (new FileSystemAccessRule ("Everyone", Filesystemrights.fullcontrol, Accesscontroltype.allow)    );
  49. Info. Setaccesscontrol (FS);
  50. }
  51. //Reload the install function
  52. public override void Install (System.Collections.IDictionary statesaver)
  53. {
  54. string server = this . context.parameters["Server"]; Server name
  55. string uid = this . context.parameters["User"]; SQL Server user name
  56. string pwd = this . context.parameters["pwd"]; Password
  57. string path = this . context.parameters["Targetdir"]; installation directory
  58. String ch = path. Substring (path.    Length-1, 1);
  59. if (ch = = @"\") //The path is processed to determine if there is ' \ ' at the end
  60. Path = path. Substring (0, path. LENGTH-1); //Have it deleted
  61. string strSQL = "server=" + server + "uid=" + uid + ";p wd=" + pwd + ";d atabase=master"; Connection database string
  62. string dataname = @"STUBOARDDB"; Database name
  63. string strmdf = path + @"xsjsglxt.mdf";     MDF file path, you need to note that the file name is the same as the database file name you just added!
  64. Setfullcontrol (Strmdf); //Set permissions to everyone
  65. string strldf = path + @"xsjsglxt_log.ldf"; LDF file path
  66. Setfullcontrol (STRLDF); //Set permissions to everyone
  67. base.    Install (statesaver);
  68. This . CreateDatabase (strSQL, Dataname, Strmdf, strldf, Path); //Start database creation
  69. }
  70. }
  71. }


Perhaps you see more code, in fact, you can reuse these, you just need to change a little bit on the line. Like (something in a black box): you know.


Well, finally, build the installation package.


Reference: Wide Field Blog Park. C # WinForm Packaging (with database installation) [Cp/ol].http://www.cnblogs.com/scottckt/archive/2011/05/14/2046313.html, 2011-05-14/2014-07-08

Hai Fang's CSDN blog. VS2010 c/S mode WinForm packaged deployment. Detailed! [cp/ol].http://blog.csdn.net/xhf55555/article/details/7702212,2012-06-29/2014-07-08

How C # WinForm program is packaged as a Setup project (diagram)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.