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