这几天忙于ASP.NET2.0安装打包,在网上找遍所有信息却不能完全满足自己的需要。现总经经验出下:
1、站点: 如果新建默认的Web安装项目,那它将创建的默认网站下的一个虚拟应用程序目录而不是一个新的站点。故我们只有创建新的安装项目,而不是Web安装项目。然后通过安装类进行自定义操作,创建新站如下图:

2、创建新的安项目之后,在(文件系统编辑器)里的应用程序文件夹里,添加ASP.net2.0的项目输出(还有第二种方法是用ASP.NET2.0发布网站到一个文件夹下面,再把这个文件夹下面所有文件复制粘贴过来,这样生成的安装文件,在安装之后就没有源代码文件了,我自己就是这样做的)如下图:
图1下图的MyPojectSetup项目下已有了Web项目的输出,以及一个SQL脚本(SQL脚本是通SQL2005生成的,将在安装类库里要使用的)

图2文件系统左边界面,添加了Web项目输出

2.2添加完项目输出之后,需要设置安装界面。我们的要求是第一、建一个新站点,所以需要所安装的IIS服务器地址,以及新站点的端口。第二、需要数据库的地址,新建的数据库名称,以及访问数据库的用户名和密码两项(需要有创库权限的)。
如图1在文件系统编辑器右边,选择用户界面,然后看到如下:

在启动选择单击右击菜单,添加对话框A,并在对话框A上单击右键=》上移到安装文件夹的上面:

再次设置右边属性,文本框A是站点信息输入如下信息,其中Edit1Property是一个需要传入安装类的参数。

按照以上方式再添加文本框B并移到文本框A的下面,如下图所示

注意:如上所示安装项目基本的事情已经做完了,但还有最后一个步骤没有做,那就是自定义操作,也就安装的重中之重的事情,安装类库的创建,如3点所示。创建安装类库之后就需要把它的输出来添加到安装项目里如同Web项目,然后设置自定义活动为这个项目就可以,详情在下面介绍。
3、需要创建一个安装类库,里面把Class1.cs删除,再添加一个新的安装文件。安装项目所有的自定义都是在这里用编码完成的(包括数据库生成,虚拟站点创建,IIS属性修改,Web.Config文件修改)。也就是说,安装项目是个外壳,通过创建一些界面接收用户参数,然后利用这个安装类库,提供的功能,进行自己的操作。


4、安装程序类新建之后,双击进入代码状态,用override重载Install函数如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;

namespace MyProjectInstall

...{
[RunInstaller(true)]
public partial class InstallerMySample : Installer

...{
public InstallerMySample()

...{
InitializeComponent();
}

public override void Install(System.Collections.IDictionary stateSaver)

...{

}
}
}
安装数据库的代码如下,其中用了两种方法,一种是用SQL命令方式创建数据库,另一种是调用osql命令执行脚本,创建数据表结构等,最后用命令追加一条记录.

数据库操作#region 数据库操作

protected void AddDBTable()

...{
try

...{
//创建数据库
ExcuteSQL("master", string.Format("CREATE DATABASE {0}", this._dataBaseName));

//调用osql执行脚本
ExcuteScript();

//添加原始数据
ExcuteSQL(_dataBaseName, "INSERT INTO GV_SystemAdmin(SystemAdminUser,SystemAdminPass,SystemAdminName,SystemAdminMemo) VALUES ('admin', 'admin', 'Administrator' ,'系统默认超级用户')");
}
catch (Exception ex)

...{
throw new ApplicationException(ex.Message);
}
}

//此方法在本例中未用到,因为最后只要追加一条记录,
//但如果有多条可以写成一个脚本,以资源的形式嵌入到这个项目
//然后用如下的方法读取,然后调用SQL命令执行
private string GetSQLScript(string fileName)

...{
try

...{
//得到当前程序集
Assembly asm = Assembly.GetExecutingAssembly();
//资源文件
Stream strm =
asm.GetManifestResourceStream(asm.GetName().Name + "." + fileName);
//读取嵌入文件内容,文本文件内容必须为Unicode
StreamReader reader = new StreamReader(strm);

return reader.ReadToEnd();
}
catch

...{
return String.Empty;
}
}

private void ExcuteSQL(string dataBaseName, string SQL)

...{
SqlConnection con =
new SqlConnection(string.Format("user id={0};password={1};Initial Catalog={2};Data Source={3};", _username, _saPassword, dataBaseName, _servername));
SqlCommand cmd = new SqlCommand(SQL, con);
cmd.Connection.Open();
cmd.Connection.ChangeDatabase(dataBaseName);

try

...{
cmd.ExecuteNonQuery();
}
finally

...{
//最后总要关闭数据库
cmd.Connection.Close();
}
}

private void ExcuteScript()

...{
try

...{
Process sqlProcess = new Process();
//调用osql必须在目标机,也就是安装的机子上要有安装SQLServer服务器
//不然找不到这个命令
sqlProcess.StartInfo.FileName = "osql.exe";
//如下所指的SQL脚本文件是打包打安装项目的文件名,
//targetPath就是在安装界面用户指定的安装目录
sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P{1} -d {2} -S {3} -i {4}VideoMeetingCreateSQL2000.sql",
_username, _saPassword, _dataBaseName, _servername, _targetPath);
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit();
sqlProcess.Close();
}
catch (Exception ex)

...{
throw ex;
}
}
#endregion
修改Web.Config配置的代码如下:

WriteWebConfig 修改web.config的连接数据库的字符串#region WriteWebConfig 修改web.config的连接数据库的字符串
private bool WriteWebConfig()

...{
System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/web.config");
if (!FileInfo.Exists)

...{
throw new InstallException("Missing config file :" + this.Context.Parameters["targetdir"] + "/web.config");
}
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(FileInfo.FullName);
bool FoundIt = false;
foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])

...{
if (Node.Name == "add")

...{
if (Node.Attributes.GetNamedItem("name").Value == "MonitorConnectionString")

...{
Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source={0};database={1};User ID={2};Password={3}", _servername, _dataBaseName, _username, _saPassword);
FoundIt = true;
}
}
}
if (!FoundIt)

...{
throw new InstallException("Error when writing the config file: web.config");
}
xmlDocument.Save(FileInfo.FullName);
return FoundIt;
}
#endregion
创建IIS站点的代码如下(注我也是从网找到的,其中CreateNewWebsit这个方法中我加了IIS参数设置的代码,也就是在创建IIS时一并设置了):
注意这一句:string fileName = Environment.GetEnvironmentVariable("windir") + @"\Microsoft.NET\Framework\v2.0.50727\ASPnet_regiis.exe";原因是当你不管是手动还是自动创建一个新站点,在它的属性页ASP.NET设置里都会有1.1版和2.0版这个选项,默认是1.1,现在我要部署2.0所以在默认情况下就不能够一步到位,安装完就可以访问执行,而要去设置成2.0才行.所以要调用FrameworkV2.0下的regiis.exe得新注册一下我们指定的虚拟目录,使它是2.0.
using System;
using System.DirectoryServices;
using System.Collections;
using System.Text.RegularExpressions;
using System.Text;
using System.Runtime;
using System.Diagnostics;

/**//**
* @author 吴海燕
* @email wuhy80-usual@yahoo.com
* 2004-6-25 第一版
*/

namespace Wuhy.ToolBox

...{

/**//// <summary>
/// 这个类是静态类。用来实现管理IIS的基本操作。
/// 管理IIS有两种方式,一是ADSI,一是WMI。由于系统限制的原因,只好选择使用ADSI实现功能。
/// 这是一个遗憾。只有等到只有使用IIS 6的时候,才有可能使用WMI来管理系统
/// 不过有一个问题就是,我现在也觉得这样的一个方法在本地执行会比较的好。最好不要远程执行。
/// 因为那样需要占用相当数量的带宽,即使要远程执行,也是推荐在同一个网段里面执行
/// </summary>
public class IISAdminLib

...{

UserName,Password,HostName的定义#region UserName,Password,HostName的定义
public static string HostName

...{
get

...{
return hostName;
}
set

...{
hostName = value;
}
}

public static string UserName

...{
get

...{
return userName;
}
set

...{
userName = value;
}
}
public static string Password

...{
get

...{
return password;
}
set

...{
if (UserName.Length <= 1)

...{
throw new ArgumentException("还没有指定好用户名。请先指定用户名");
}
password = value;
}
}

public static void RemoteConfig(string hostName, string userName, string password)

...{
HostName = hostName;
UserName = userName;
Password = password;
}
private static string hostName = "localhost";
private static string userName;
private static string password;
#endregion


根据路径构造Entry的方法#region 根据路径构造Entry的方法

/**//// <summary>
/// 根据是否有用户名来判断是否是远程服务器。
/// 然后再构造出不同的DirectoryEntry出来
/// </summary>
/// <param name="entPath">DirectoryEntry的路径</param>
/// <returns>返回的是DirectoryEntry实例</returns>
public static DirectoryEntry GetDirectoryEntry(string entPath)

...{
DirectoryEntry ent;
if (UserName == null)

...{
ent = new DirectoryEntry(entPath);
}
else

...