最近做安装文件,尽管msdn上有完备的Setup project的介绍,但有些tips需要牢记:
前提:建立好Setup Project
1. 添加项目的时候,可以添加为同个sln下的其它proj的输出,这样IDE会自动添加该输出所依赖的所有dependency。但是由于我的exe还要混淆一下,所以只好手动添加该exe以及没有被识别到的一些文件,如config等。
2. 关于Custom Action。可以在View->Editor中选择Custom Action Editor,添加。添加后的内容属性里有一项CustomActionData很重要,没有就算了,如果有的话,在不小心删除了该CustomAction后,还得重新添加,此时CustomActionData不会自己复制,要记录下原来的内容,然后重新建立,然后再写上去。
3. 创建CustomAction很简单,只需要继承System.Configuration.Install.Installer,然后写上属性[RunInstaller(true)],然后override Install:
[RunInstaller(true)]
public partial class DBInstall : System.Configuration.Install.Installer
{
#region 参数
public static string _serverName { get; set; }
public static string _dbName { get; set; }
public static string _userName { get; set; }
public static string _password { get; set; }
public static bool UseSQLAuthenti { get; set; }
private string _setupType { get; set; }
private string _targetDir { get; set; }
/// <summary>
/// 资源中创建表结构及数据的文件名
/// </summary>
private const string _StructureAndDataFileName = "CreateStructureData";
#endregion
InstallCommon Installer = new InstallCommon();
public DBInstall()
{
InitializeComponent();
}
private string StripDir(string fullPath)
{
string retValue = default(string);
if (fullPath != null && fullPath != string.Empty && fullPath != default(string))
{
retValue = fullPath.Substring(0, fullPath.LastIndexOf(@"\"));
}
return retValue;
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
//数据库配置 界面
frmDBSettings dbFrom = new frmDBSettings();
DialogResult DialogResult = dbFrom.ShowDialog();
if (DialogResult != DialogResult.OK)
{
throw new InstallException("用户取消安装!");
}
SqlConnection connection = null;
InstallCommon._serverName = _serverName;
if (UseSQLAuthenti)
{
InstallCommon._userName = _userName;
InstallCommon._password = _password;
connection = Installer.TestConnection(_serverName, "master", _userName, _password);
Installer.Authenti = AuthentiMode.SQLAuthenti;
}
else
{
connection = Installer.TestConnection(_serverName, "master");
Installer.Authenti = AuthentiMode.WindowsAuthenti;
}
Installer._targetDir = StripDir(Context.Parameters["AssemblyPath"]);
Installer.ModifyConfig();
Installer.CloseConnection(connection);
}
}
4. 在CustomAction中获取TargetDir据说有一点tricky,有个老外说获取不到,只能得到AssemblyPath,为了稳妥起见,听人劝吃饱饭,懒得去试了,照搬他的方法,(StripDir)
5. 安装时写注册表。View->Editor->Registry Editor可以往注册表里写东西。本以为要自己在CustomAction里做的,没想到连这个Windows Installer都做好了,真不错,卸载的时候也自动卸掉。如果要在Value里写上Installer的环境变量,在添加键值的时候要选择Environment String Value,然后用中括号括上变量名就行了,关于Installer的所有变量:http://msdn.microsoft.com/en-us/library/aa370905(v=vs.85).aspx
未完待续。。。