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
- Using System;
- Using System.Collections;
- Using System.Collections.Generic;
- Using System.ComponentModel;
- Using System.Configuration.Install;
- Using System.Linq;
- Using System.Data.SqlClient;
- Using System.Windows.Forms;
- Using System.IO;
- Using System.Security.AccessControl;
- Namespace Installdb
- {
- [Runinstaller (true)]
- Public partial class InstallDB:System.Configuration.Install.Installer
- {
- Public installdb ()
- {
- InitializeComponent ();
- }
- //Create a database
- private void CreateDatabase (string strSQL, string dataname, string strmdf, string strldf, String path)
- {
- SqlConnection myconn = new SqlConnection (strSQL);
- String str = null;
- Try
- {
- str = @"EXEC sp_attach_db @dbname = '" + dataname + "', @filename1 = '" + strmdf + "', @filename2 = '" + strldf + "'";
- SqlCommand mycommand = new SqlCommand (str, myconn);
- MyConn.Open ();
- Mycommand.executenonquery ();
- MessageBox.Show ("Database installation succeeded! Click OK to continue "); //need using System.Windows.Forms
- }
- catch (Exception e)
- {
- MessageBox.Show ("Database installation failed! "+ e.message + " \ n "+" you can manually append data ");
- System.Diagnostics.Process.Start (path); //Open the installation directory
- }
- finally
- {
- Myconn.close ();
- }
- }
- //Rights Management
- private static void Setfullcontrol (string path)
- {
- FileInfo info = new FileInfo (path);
- FileSecurity fs = info. GetAccessControl ();
- Fs. Addaccessrule (new FileSystemAccessRule ("Everyone", Filesystemrights.fullcontrol, Accesscontroltype.allow) );
- Info. Setaccesscontrol (FS);
- }
- //Reload the install function
- public override void Install (System.Collections.IDictionary statesaver)
- {
- string server = this . context.parameters["Server"]; Server name
- string uid = this . context.parameters["User"]; SQL Server user name
- string pwd = this . context.parameters["pwd"]; Password
- string path = this . context.parameters["Targetdir"]; installation directory
- String ch = path. Substring (path. Length-1, 1);
- if (ch = = @"\") //The path is processed to determine if there is ' \ ' at the end
- Path = path. Substring (0, path. LENGTH-1); //Have it deleted
- string strSQL = "server=" + server + "uid=" + uid + ";p wd=" + pwd + ";d atabase=master"; Connection database string
- string dataname = @"STUBOARDDB"; Database name
- 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!
- Setfullcontrol (Strmdf); //Set permissions to everyone
- string strldf = path + @"xsjsglxt_log.ldf"; LDF file path
- Setfullcontrol (STRLDF); //Set permissions to everyone
- base. Install (statesaver);
- This . CreateDatabase (strSQL, Dataname, Strmdf, strldf, Path); //Start database creation
- }
- }
- }
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)