1、 發佈項目。
2、 新建—》項目---》類庫 取名:SetupClassLibrary
右擊類庫—添加—安裝程序類 MyInstaller
MyInstaller類有創建虛擬目錄、附加數據庫、修改webconfig等功能。在類中編寫如下的代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.Management;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Collections.Specialized;
using System.ServiceProcess;
using System.Windows.Forms;
namespace SetupClassLibrary
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
//先設置私有成員,對應安裝程序裏接受到得用戶輸入
private string dbname;
private string dbserver;
private string user;
private string pwd;
private string iis;
private string physicaldir;
private string virtualdir;
private string rdovisit;
public static string VirDirSchemaName = "IIsWebVirtualDir";
private string _target;
private DirectoryEntry _iisServer;
private ManagementScope _scope;
private ConnectionOptions _connection;
public MyInstaller()
{
InitializeComponent();
}
#region WriteWebConfig 修改web.config的連接數據庫的字符串 WriteWebConfig 修改web.config的連接數據庫的字符串
private void WriteWebConfig()
{
//加載配置文件
System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/Web.config");
if (!FileInfo.Exists)
{
throw new InstallException("缺少配置文件 :" + this.Context.Parameters["targetdir"] + "/Web.config");
}
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(FileInfo.FullName);
//修改連接字符串
foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
{
if (Node.Name == "add")
{
if (Node.Attributes.GetNamedItem("name").Value == "ConnectionString")
{
//if (rdovisit == "1")
//{
// Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source=" + dbserver + ";Initial Catalog=" + dbname + ";Integrated Security=True");
//}
//else
//{
Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source=" + dbserver + ";Initial Catalog=" + dbname + ";User ID=" + user + ";pwd=" + pwd + ";");
// }
}
}
}
xmlDocument.Save(FileInfo.FullName);
}
#endregion
#region 創建虛擬目錄 創建虛擬目錄
private void CreateVirtualDir()
{
string constIISWebSiteRoot = "IIS://" + iis + "/W3SVC/1/ROOT";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualdir, root.SchemaClassName);
tbEntry.Properties["Path"][0] = physicaldir;//設置物理地址
tbEntry.Invoke("AppCreate", true);
//tbEntry.Properties["DefaultDoc"][0] = "Default.aspx";//設置起始頁
tbEntry.CommitChanges();
}
#endregion
#region Uninstall 刪除 Uninstall 刪除
public override void Uninstall(IDictionary savedState)
{
//添加自定義的卸載代碼
if (savedState == null)
{
throw new ApplicationException("未能卸載!");
}
else
{
base.Uninstall(savedState);
}
}
#endregion
public override void Install(System.Collections.IDictionary mySavedState)
{
base.Install(mySavedState);
physicaldir = this.Context.Parameters["targetdir"].ToString();
virtualdir = this.Context.Parameters["virtualdir"].ToString();
dbname = this.Context.Parameters["dbname"].ToString();
dbserver = this.Context.Parameters["dbserver"].ToString();
user = this.Context.Parameters["user"].ToString();
rdovisit = this.Context.Parameters["rdovisit"].ToString();
pwd = this.Context.Parameters["pwd"].ToString();
iis = this.Context.Parameters["iis"].ToString();
string strSql = "";
//if (rdovisit == "1") //如果選中的是windows身份驗證
//{
strSql = "Data Source=" + dbserver + ";Initial Catalog=master;User ID=" + user + ";pwd=" + pwd + ";";//連接數據庫字符串
string strMdf = physicaldir + @"SharpRush.mdf";//MDF文件路徑,這裏需注意文件名要與剛添加的數據庫文件名一樣!
string strLdf = physicaldir + @"SharpRush.ldf";//LDF文件路徑
this.CreateDataBase(strSql, dbname, strMdf, strLdf, physicaldir);//開始創建數據庫
// 創建虛擬目錄
CreateVirtualDir();
// 修改web.config
WriteWebConfig();
//}
//else //如果選中的是sqlserver身份驗證
//{
//strSql = "Data Source=" + dbserver + ";Initial Catalog=master;User ID=" + user + ";pwd=" + pwd + ";";//連接數據庫字符串
//string strMdf = physicaldir + @"SharpRush.mdf";//MDF文件路徑,這裏需注意文件名要與剛添加的數據庫文件名一樣!
//string strLdf = physicaldir + @"SharpRush.ldf";//LDF文件路徑
//this.CreateDataBase(strSql, dbname, strMdf, strLdf, physicaldir);//開始創建數據庫
//// 創建虛擬目錄
//CreateVirtualDir();
//// 修改web.config
//WriteWebConfig();
//}
}
/// <summary>
/// 附加數據庫方法
/// </summary>
/// <param name="strSql">連接數據庫字符串,連接master系統數據庫</param>
/// <param name="DataName">數據庫名字</param>
/// <param name="strMdf">數據庫文件MDF的路徑</param>
/// <param name="strLdf">數據庫文件LDF的路徑</param>
/// <param name="path">安裝目錄</param>
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("數據庫安裝成功!點擊確定繼續");//需Using System.Windows.Forms
}
catch (Exception e)
{
MessageBox.Show("數據庫已存在,數據庫安裝失敗!" + e.Message + "\n\n" + "請先分離數據庫!");
System.Diagnostics.Process.Start(path);//打開安裝目錄
}
finally
{
myConn.Close();
}
}
}
}
3、右擊項目解決方案—添加—新建項目
輸入名稱,選擇存儲路徑。
右擊”應用程序文件夾”---添加---項目輸出
選擇主輸出,點擊確定按鈕。
將項目發佈出的文件複製到“應用程序文件夾”,同時複製數據庫到該文件夾下。右擊“應用程序文件夾”--添加—選擇路徑“C:\Program Files\Internet Explorer”下的“iexplore.exe”文件(該文件是ie應用程序),再添加 路徑“C:\WINDOWS\system32”下的“msiexec.exe”文件。(該文件是卸載程序的應用文件)。
然後右擊“應用程序文件夾”下的“iexplore.exe” 文件和“msiexec.exe”文件,創建快捷方式並命名。將快捷方式拖到---用戶的”程序”菜單文件夾下 ,再創建一個“iexplore.exe” 文件的快捷方式拖動到“用戶菜單”文件夾下---右擊快捷方式修改屬性。其中“msiexec.exe”文件快捷方式的Arguments屬性要修改爲 /x produccode
4、 右擊安裝項目---試圖—用戶界面
右擊安裝下的啓動節點—添加對話框
添加“文本框(A)”和“文本框(B)”
右擊修改兩個文本框的屬性
文本框(A)