图文描述Vs2005制作WEB应用程序安装包的方法[E8.Net正式用户可以找我们获取全部代码参考]
Posted on 2008-03-22 17:19 苏康胜 阅读(167) 评论(0) 编辑 收藏。NET工具安装工程向导很快速的就可以实现一个WEB应用程序的安装制作,如果提供给终端用户使用,安装包主要还要做的事情大概有:
1 、配置web.config
2、 安装数据库
3、 创建快捷方式
4、 创建开始程序菜单
。。。。。。。。
创建一个安装工程,并设置好各种属性后。我们可以开始安装包的一些扩展功能。
首先我们需要编写一个E8Installer 类 继承 Installer ,通过重载 Installer类的 install uninstall ...方法来实现扩展
public override void Install(System.Collections.IDictionary stateSaver)
{
try
{
base.Install(stateSaver);
if (!System.Diagnostics.EventLog.SourceExists("EpowerInstall"))
{
System.Diagnostics.EventLog.CreateEventSource(
"EpowerInstall", "");
}
myLog.Source = "EpowerInstall";
System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
string strTemp = Asm.Location;
System.Diagnostics.Debug.WriteLine(strTemp);
strFolderName = strTemp.Remove(strTemp.LastIndexOf(@"\"), strTemp.Length - strTemp.LastIndexOf(@"\"));
//确保IIS为正确的版本
InstallToVersion2(strFolderName.Substring(strFolderName.LastIndexOf(@"\") + 1));
//this.InstallMyConfig(stateSaver);//调用上面的方法
this.InstallMyConfigTemp(stateSaver);
//创建桌面快捷方式
CreateE8UrlShortCut(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), strFolderName.Substring(strFolderName.LastIndexOf(@"\") + 1));
stateSaver.Add("E8HelpDeskDeskSC", System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url" ); //保存桌面快诫方式文件绝对地址
//创建程序快截方式
string strStartMenue = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
if (File.Exists(strStartMenue + "\\E8.HelpDesk服务管理系统.url"))
{
File.Copy(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url", strStartMenue + "\\E8.HelpDesk服务管理系统.url", true);
}
else
{
File.Copy(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url", strStartMenue + "\\E8.HelpDesk服务管理系统.url", false);
}
stateSaver.Add("E8StartMenueSC", strStartMenue + "\\E8.HelpDesk服务管理系统.url"); //保存开始菜单文件绝对地址
//安装数据库
InstallDataBase();
}
catch
{
throw;
}
}
public override void Uninstall(System.Collections.IDictionary stateSaver)
{
//删除快捷键
if (File.Exists(stateSaver["E8HelpDeskDeskSC"].ToString()))
{
File.Delete(stateSaver["E8HelpDeskDeskSC"].ToString());
}
//删除开始菜单
if (File.Exists(stateSaver["E8StartMenueSC"].ToString()))
{
File.Delete(stateSaver["E8StartMenueSC"].ToString());
}
base.Uninstall(stateSaver);
}
{
try
{
base.Install(stateSaver);
if (!System.Diagnostics.EventLog.SourceExists("EpowerInstall"))
{
System.Diagnostics.EventLog.CreateEventSource(
"EpowerInstall", "");
}
myLog.Source = "EpowerInstall";
System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
string strTemp = Asm.Location;
System.Diagnostics.Debug.WriteLine(strTemp);
strFolderName = strTemp.Remove(strTemp.LastIndexOf(@"\"), strTemp.Length - strTemp.LastIndexOf(@"\"));
//确保IIS为正确的版本
InstallToVersion2(strFolderName.Substring(strFolderName.LastIndexOf(@"\") + 1));
//this.InstallMyConfig(stateSaver);//调用上面的方法
this.InstallMyConfigTemp(stateSaver);
//创建桌面快捷方式
CreateE8UrlShortCut(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop), strFolderName.Substring(strFolderName.LastIndexOf(@"\") + 1));
stateSaver.Add("E8HelpDeskDeskSC", System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url" ); //保存桌面快诫方式文件绝对地址
//创建程序快截方式
string strStartMenue = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
if (File.Exists(strStartMenue + "\\E8.HelpDesk服务管理系统.url"))
{
File.Copy(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url", strStartMenue + "\\E8.HelpDesk服务管理系统.url", true);
}
else
{
File.Copy(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\E8.HelpDesk服务管理系统.url", strStartMenue + "\\E8.HelpDesk服务管理系统.url", false);
}
stateSaver.Add("E8StartMenueSC", strStartMenue + "\\E8.HelpDesk服务管理系统.url"); //保存开始菜单文件绝对地址
//安装数据库
InstallDataBase();
}
catch
{
throw;
}
}
public override void Uninstall(System.Collections.IDictionary stateSaver)
{
//删除快捷键
if (File.Exists(stateSaver["E8HelpDeskDeskSC"].ToString()))
{
File.Delete(stateSaver["E8HelpDeskDeskSC"].ToString());
}
//删除开始菜单
if (File.Exists(stateSaver["E8StartMenueSC"].ToString()))
{
File.Delete(stateSaver["E8StartMenueSC"].ToString());
}
base.Uninstall(stateSaver);
}
一、配置web.config
刚才我们看到了 Override void Install 方法中有代码配置web.config
this.InstallMyConfigTemp(stateSaver);
需要有数据接口, 我们可以打开安装工程的 用户界面视图,添加页面 可以作为 参数的入口
如图:
添加了用户界面后,打开自定义操作视图,添加自定义操作,如图:
这样可以实现参数的传入了。
配置web.config的方法代码参考如下:
sDBServer = Context.Parameters["dbserver"].ToString();
sDBName = Context.Parameters["dbname"];
strDBSAPsw = Context.Parameters["dbsapsw"];
sCompanyName = Context.Parameters["companyname"];
// sDBUser = "epuser";
sDBPsw = Context.Parameters["password"];
string strWorkTimeType = Context.Parameters["WorkTimeType"].ToString();
string strOnDutyTime1 = Context.Parameters["OnDutyTime1"];
string strOffDutyTime1 = Context.Parameters["OffDutyTime1"];
string strOnDutyTime2 = Context.Parameters["OnDutyTime2"];
string strOffDutyTime2 = Context.Parameters["OffDutyTime2"];
string strTempCataLog = Context.Parameters["filepath"] + @"\Upload\";
string strFileCataLog = Context.Parameters["filepath"] + @"\Download\";
string strGoLimit = Context.Parameters["golimit"];
string strOverLimit = Context.Parameters["overlimit"];
strDBOption = Context.Parameters["dboption"];
strCheckMsg = CheckTime(strOnDutyTime1, strOffDutyTime1, strOnDutyTime2, strOffDutyTime2);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
strCheckMsg = CheckPathValid(strTempCataLog, strFileCataLog);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
strCheckMsg = CheckNumbericSet(strGoLimit, strOverLimit);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
if (strWorkTimeType == "1")
strWorkTimeType = "50";
if (strWorkTimeType == "2")
strWorkTimeType = "55";
if (strWorkTimeType == "3")
strWorkTimeType = "60";
string strValue = "server=" + sDBServer.Trim() + ";user id=" + sDBUser.Trim() +
";password=" + sDBPsw.Trim() + ";database=" + sDBName.Trim() + ";max pool size=100";
System.IO.FileInfo fileInfo = new System.IO.FileInfo(strFolderName + "\\web.config");
if (!fileInfo.Exists)
{
throw new InstallException("web.config文件不存在!" + strFolderName + "\\web.config");
}
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(fileInfo.FullName);
bool blnFindit = false;
foreach (XmlNode xmlNode in xmlDoc["configuration"]["connectionStrings"].ChildNodes)
{
if (xmlNode.Name == "add")
{
if (xmlNode.Attributes["name"].Value == "SQLConnString")
{
xmlNode.Attributes["connectionString"].Value = strValue;
blnFindit = true;
}
}
}
foreach (XmlNode xmlNode in xmlDoc["configuration"]["appSettings"].ChildNodes)
{
if (xmlNode.Name == "add")
{
if (xmlNode.Attributes["key"].Value == "SQLConnString")
{
xmlNode.Attributes["value"].Value = strValue;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "WorkTimeType")
{
xmlNode.Attributes["value"].Value = strWorkTimeType;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OnDutyTime1")
{
xmlNode.Attributes["value"].Value = strOnDutyTime1;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OffDutyTime1")
{
xmlNode.Attributes["value"].Value = strOffDutyTime1;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OnDutyTime2")
{
xmlNode.Attributes["value"].Value = strOnDutyTime2;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OffDutyTime2")
{
xmlNode.Attributes["value"].Value = strOffDutyTime2;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "TempCataLog")
{
xmlNode.Attributes["value"].Value = strTempCataLog;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "FileCataLog")
{
xmlNode.Attributes["value"].Value = strFileCataLog;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OverTimeLimit")
{
xmlNode.Attributes["value"].Value = strOverLimit;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "SqlCacheDataSource")
{
xmlNode.Attributes["value"].Value = sDBName;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "ZH_ServiceTimeLimit")
{
xmlNode.Attributes["value"].Value = strGoLimit;
blnFindit = true;
}
}
}
if (blnFindit == true)
{
xmlDoc.Save(fileInfo.FullName);
}
}
catch
{
throw;
}
finally
{
}
sDBName = Context.Parameters["dbname"];
strDBSAPsw = Context.Parameters["dbsapsw"];
sCompanyName = Context.Parameters["companyname"];
// sDBUser = "epuser";
sDBPsw = Context.Parameters["password"];
string strWorkTimeType = Context.Parameters["WorkTimeType"].ToString();
string strOnDutyTime1 = Context.Parameters["OnDutyTime1"];
string strOffDutyTime1 = Context.Parameters["OffDutyTime1"];
string strOnDutyTime2 = Context.Parameters["OnDutyTime2"];
string strOffDutyTime2 = Context.Parameters["OffDutyTime2"];
string strTempCataLog = Context.Parameters["filepath"] + @"\Upload\";
string strFileCataLog = Context.Parameters["filepath"] + @"\Download\";
string strGoLimit = Context.Parameters["golimit"];
string strOverLimit = Context.Parameters["overlimit"];
strDBOption = Context.Parameters["dboption"];
strCheckMsg = CheckTime(strOnDutyTime1, strOffDutyTime1, strOnDutyTime2, strOffDutyTime2);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
strCheckMsg = CheckPathValid(strTempCataLog, strFileCataLog);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
strCheckMsg = CheckNumbericSet(strGoLimit, strOverLimit);
if (strCheckMsg != "")
{
throw new InstallException(strCheckMsg);
}
if (strWorkTimeType == "1")
strWorkTimeType = "50";
if (strWorkTimeType == "2")
strWorkTimeType = "55";
if (strWorkTimeType == "3")
strWorkTimeType = "60";
string strValue = "server=" + sDBServer.Trim() + ";user id=" + sDBUser.Trim() +
";password=" + sDBPsw.Trim() + ";database=" + sDBName.Trim() + ";max pool size=100";
System.IO.FileInfo fileInfo = new System.IO.FileInfo(strFolderName + "\\web.config");
if (!fileInfo.Exists)
{
throw new InstallException("web.config文件不存在!" + strFolderName + "\\web.config");
}
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(fileInfo.FullName);
bool blnFindit = false;
foreach (XmlNode xmlNode in xmlDoc["configuration"]["connectionStrings"].ChildNodes)
{
if (xmlNode.Name == "add")
{
if (xmlNode.Attributes["name"].Value == "SQLConnString")
{
xmlNode.Attributes["connectionString"].Value = strValue;
blnFindit = true;
}
}
}
foreach (XmlNode xmlNode in xmlDoc["configuration"]["appSettings"].ChildNodes)
{
if (xmlNode.Name == "add")
{
if (xmlNode.Attributes["key"].Value == "SQLConnString")
{
xmlNode.Attributes["value"].Value = strValue;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "WorkTimeType")
{
xmlNode.Attributes["value"].Value = strWorkTimeType;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OnDutyTime1")
{
xmlNode.Attributes["value"].Value = strOnDutyTime1;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OffDutyTime1")
{
xmlNode.Attributes["value"].Value = strOffDutyTime1;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OnDutyTime2")
{
xmlNode.Attributes["value"].Value = strOnDutyTime2;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OffDutyTime2")
{
xmlNode.Attributes["value"].Value = strOffDutyTime2;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "TempCataLog")
{
xmlNode.Attributes["value"].Value = strTempCataLog;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "FileCataLog")
{
xmlNode.Attributes["value"].Value = strFileCataLog;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "OverTimeLimit")
{
xmlNode.Attributes["value"].Value = strOverLimit;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "SqlCacheDataSource")
{
xmlNode.Attributes["value"].Value = sDBName;
blnFindit = true;
}
if (xmlNode.Attributes["key"].Value == "ZH_ServiceTimeLimit")
{
xmlNode.Attributes["value"].Value = strGoLimit;
blnFindit = true;
}
}
}
if (blnFindit == true)
{
xmlDoc.Save(fileInfo.FullName);
}
}
catch
{
throw;
}
finally
{
}
二、安装数据库
安装数据库主要是通过执行数据库命令来实现了,代码参考如下:
//创建数据库
sb = this.GetSqlFile("createdatabase.txt");
sb.Replace("@dbname@", sDBName);
sb.Replace("@password@", sDBPsw);
sb.Replace("@dbuser@", sDBUser);
ExecuteSql(connStr, "master", sb.ToString());
//创建数据库用户
sb = this.GetSqlFile("createdbuser.txt");
sb.Replace("@dbname@", sDBName);
sb.Replace("@password@", sDBPsw);
sb.Replace("@dbuser@", sDBUser);
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
//空数据库
sb = this.GetSqlFile("tables.sql");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
//空数据库所有函数
sb = this.GetSqlFile("funGetFullDept.txt");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
sb = this.GetSqlFile("funGetFullCatalog.txt");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
sb = this.GetSqlFile("createdatabase.txt");
sb.Replace("@dbname@", sDBName);
sb.Replace("@password@", sDBPsw);
sb.Replace("@dbuser@", sDBUser);
ExecuteSql(connStr, "master", sb.ToString());
//创建数据库用户
sb = this.GetSqlFile("createdbuser.txt");
sb.Replace("@dbname@", sDBName);
sb.Replace("@password@", sDBPsw);
sb.Replace("@dbuser@", sDBUser);
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
//空数据库
sb = this.GetSqlFile("tables.sql");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
//空数据库所有函数
sb = this.GetSqlFile("funGetFullDept.txt");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
sb = this.GetSqlFile("funGetFullCatalog.txt");
ExecuteSql(connStr, sDBName.Trim(), sb.ToString());
三、创建快捷方式
由于是WEB 安装,安装过程中可以选择设置 WEB站点和虚拟路径名称,因此还不能直接以现有文件的方式生成快捷方式。 可以通过代码生成文件并复制到相关目录下的方式来实现。代码参考:
System.IO.StreamWriter writer = System.IO.File.CreateText(sSpecFolder + "\\E8.HelpDesk服务管理系统.url");
writer.WriteLine("[internetshortcut]");
writer.WriteLine("URL=http://localhost/" + strVirtualD);
writer.Close();
既然是自己代码生成,还可以写入指定图标。。。。节点writer.WriteLine("[internetshortcut]");
writer.WriteLine("URL=http://localhost/" + strVirtualD);
writer.Close();
不过需要注意的是,安装程序生成的文件,需要 重载 uninstaller 方法 中处理删除动作
一个完整的安装包还需要考虑更多因素,比如 安装环境存在多个 framework版本情况的处理等。限于篇幅不多介绍,希望能给大家的安装包开发起到抛砖引玉的作用
E8.Net工作流平台为.Net企业应用开发提供了一个起点。E8.Net工作流提升企业战略执行力,欢迎访问: http://www.feifanit.com.cn/productFlow.htm