应该很多人都有遇到这个问题.现在给个完整的解决方案.
1.添加一个安装项目(当然你的其他项目应该都已经OK了.现在我们已经做好了一个WinUI的项目.记得要建立App.config文件,而且要有连接字符串的配置节存在.不然之后会出错.因为我没做错误处理.)
目前的App.config文件内容,connectionString值为空,需要用户在安装时输入.
?
- <?xml version="1.0" encoding="utf-8" ?>
? ?
- <configuration>
? ?
- ??<connectionStrings>
? ?
- ? ? <add name="ConnectionString" connectionString="" providerName="System.Data.SqlClient" />
? ?
- ??</connectionStrings>
? ?
- </configuration>
?
复制代码


2.在安装项目中添加项目输出,如UI层,业务逻辑层,数据操作层,实体层,公共层等项目输出.

3.选中左面的用户桌面,在右边右键创建新的快捷方式.




4.生成安装项目,就可以生成安装程序了.不过还没完呢.


5.安装过程中需要用户输入连接数据库的信息或建立数据库等操作.需要对安装过程进行定制.在用户界面视图中进行.

6.添加让用户输入信息的对话框

7.修改文本框的属性.最多可以有4个文本框.定义窗口的标题(BannerText),窗口的描述信息(BodyText),各文本框标签的值(Edit_Label),对应的属性名(Edit_Property),默认值(Edit_Value),是否可见(Edit_Visible)等属性.

8.要新建一个类库项目来实现定制安装的功能.


9.建立功能实现类


10.代码如下(请注意看代码注释):
?
- using System;
? ?
- using System.Collections.Generic;
? ?
- using System.ComponentModel;
? ?
- using System.Configuration.Install;
? ?
- using System.Configuration;
? ?
- using System.Xml;
? ?
- using System.IO;
? ?
-
? ?
- namespace SetSetup
? ?
- {
? ?
- ? ? /// <summary>
? ?
- ? ? /// 继承安装类
? ?
- ? ? /// www.szitr.com
? ?
- ? ? /// </summary>
? ?
- ? ? [RunInstaller(true)]
? ?
- ? ? public partial class SetSqlConStr : Installer
? ?
- ? ? {
? ?
- ? ?? ???public SetSqlConStr()
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?InitializeComponent();
? ?
- ? ?? ???}
? ?
- ? ?? ???/// <summary>
? ?
- ? ?? ???/// 重写基类的安装方法
? ?
- ? ?? ???/// </summary>
? ?
- ? ?? ???/// <param name="stateSaver"></param>
? ?
- ? ?? ???public override void Install(System.Collections.IDictionary stateSaver)
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?base.Install(stateSaver);
? ?
- ? ?? ?? ?? ?//得到用户输入的参数
? ?
- ? ?? ?? ?? ?//参数来自于后面 第 14 步 自定义操作的参数
? ?
- ? ?? ?? ?? ?//自定义操作参数的属性CustomActionData:/SqlServerIP=[SQLSERVER_NAME] /DataBase=[DATABASE_NAME] /UserName=[USERNAME] /Password=[PASSWORD] /TargetDir="[TARGETDIR]/"
? ?
- ? ?? ?? ?? ?//中括号中的就是之前各文本框的Edit_Property值.最后一个TARGETDIR是安装路径,注意后面还有个反斜杆
? ?
- ? ?? ?? ?? ?string sqlServerIP = this.Context.Parameters["SqlServerIP"];
? ?
- ? ?? ?? ?? ?string database = this.Context.Parameters["DataBase"];
? ?
- ? ?? ?? ?? ?string userName = this.Context.Parameters["UserName"];
? ?
- ? ?? ?? ?? ?string password = this.Context.Parameters["Password"];
? ?
- ? ?? ?? ?? ?string targetdir = this.Context.Parameters["TargetDir"];
? ?
- ? ?? ?? ?? ?//这里写你要执行的代码
? ?
- ? ?? ?? ?? ?//组合连接字符串
? ?
- ? ?? ?? ?? ?string conString = String.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};Persist Security Info=True",sqlServerIP,database,userName,password);
? ?
- ? ?? ?? ?? ?//更新连接字串设定值,WinUI.exe.config 要改成你设定档的名称
? ?
- ? ?? ?? ?? ?UpdateConConfig("ConnectionString",conString,targetdir + "WinUI.exe.config");
? ?
- ? ?? ???}
? ?
-
? ?
- ? ?? ???/// <summary>
? ?
- ? ?? ???/// 修改设定档连接字符串的值
? ?
- ? ?? ???/// </summary>
? ?
- ? ?? ???/// <param name="conName">连接字符串名称</param>
? ?
- ? ?? ???/// <param name="conString">连接字符串</param>
? ?
- ? ?? ???/// <param name="configfilePath">设定档路径及名称</param>
? ?
- ? ?? ???public static void UpdateConConfig(string conName,string conString,string configfilePath)
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?XmlDocument xmlDoc = new XmlDocument();
? ?
- ? ?? ?? ?? ?//读取设定档
? ?
- ? ?? ?? ?? ?xmlDoc.Load(configfilePath);
? ?
- ? ?? ?? ?? ?//取得连接字符串的节点
? ?
- ? ?? ?? ?? ?XmlNode xmlNode = xmlDoc.SelectSingleNode("configuration/connectionStrings/add[@name='" + conName + "']");
? ?
- ? ?? ?? ?? ?//修改连接字符串
? ?
- ? ?? ?? ?? ?xmlNode.Attributes["connectionString"].InnerText = conString;
? ?
- ? ?? ?? ?? ?//保存
? ?
- ? ?? ?? ?? ?xmlDoc.Save(configfilePath);
? ?
- ? ?? ???}
? ?
-
? ?
- ? ?? ???public override void Uninstall(System.Collections.IDictionary savedState)
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?base.Uninstall(savedState);
? ?
- ? ?? ???}
? ?
- ? ?? ???public override void Commit(System.Collections.IDictionary savedState)
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?base.Commit(savedState);
? ?
- ? ?? ???}
? ?
- ? ?? ???public override void Rollback(System.Collections.IDictionary savedState)
? ?
- ? ?? ???{
? ?
- ? ?? ?? ?? ?base.Rollback(savedState);
? ?
- ? ?? ???}
? ?
- ? ? }
? ?
- }
?
复制代码
11.回到安装项目.打开自定义操作视图

12.在安装中添加自定义操作

13.添加刚刚第9步所建立的项目的主输出

14.修改此自定义操作的CustomActionData属性
CustomActionData:/SqlServerIP=[SQLSERVER_NAME]/DataBase=[DATABASE_NAME] /UserName=[USERNAME] /Password=[PASSWORD]/TargetDir="[TARGETDIR]/"

15.重新生成一次.再执行安装程序看看.已经OK啦!

最后看看我们的设定档内容
?
- <?xml version="1.0" encoding="utf-8"?>
? ?
- <configuration>
? ?
- ??<connectionStrings>
? ?
- ? ? <add name="ConnectionString" connectionString="Data Source=DBServer;Initial Catalog=szitrDB;User ID=szitr.com;Password=szitr.com;Persist Security Info=True" providerName="System.Data.SqlClient" />
? ?
- ??</connectionStrings>
? ?
- </configuration>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|