C#再打包安装时动态配置文件

本文介绍了一个项目中自定义安装过程的方法,包括数据库的附加及App.config和Web.config配置文件的自动化修改流程。

最近在做一个项目时,同进涉及WinFromWeb的安装过程,并且两上项目都有各自配置文件,分别为App.configWeb.config

导入数据库成为第一件头痛的事,而后又要手动配置文件,非常麻烦。如何能让程序在安装的过程中也同时修改相应的配置文件。

安装部署主要分以下几个流程:

一、创建一个工程,用来对附加数据库和修改配置文件

二、自定义安装部署

 

一、创建一个类库文件,工程名称为:InstallDB

        并且给工程添加System.Configuration.InstallSystem.Xml引用

        添加一个类,为InstallDB.cs(主要是用来附加数据库)以及修改配置文件

     另外一个类AppConfig.cs,则主要负责对配置文件也就是XML文件操作。InstallDB类和AppConfig的源代码在尾部显示

 

二、创建一个Web安装项目包

       在该项目中选择文件系统视图中的Web 应用程序文件夹中添加Web网页项目

  并且在文件系统视图中,右击目标计算机上的文件夹――添加特殊文件夹――Program Files 文件夹――创建自己的主程序的文件名称,并且将WinForm程序添加进来。同时在该文件夹的中创建一个文件侠,名称为:database(将数据库文件添加过去)

1.       切换到用户界面视图,在用户界面编辑器中,选择“安装”下的“启动”节点。在“操作”菜单上,选择“添加对话框”。

2.       在“添加对话框”对话框中选择“文本框 (A)”对话框。

3.       在“操作”菜单上选择“上移”。重复此步骤,直到“文本框 (A)”对话框位于“安装文件夹”节点之上。

4.       在“属性”窗口中,选择 BannerText 属性并键入 自定义数据库安装

5.       选择 BodyText 属性并键入自定义数据库安装

6.       选择 Edit1Label 属性并键入服务器名:

7.       选择 Edit1Property 属性并键入 SERVERNAME

8.       选择 Edit2Label 属性并键入数据库管理员帐号:

9.       选择 Edit2Property 属性并键入 UID

10.    选择 Edit3Label 属性并键入管理员密码:

11.    选择 Edit3Property 属性并键入 PWD

1.       在“视图”菜单上指向“编辑器”,然后选择“自定义操作”。

2.       在自定义操作编辑器中选择“安装”节点。在“操作”菜单上,选择“添加自定义操作”。

3.       在“选择项目中的项”对话框中,双击“应用程序文件夹”。

4.       选择“主输出来自 InstallDB(活动)”项。

在“属性”窗口中,选择 CustomActionData 属性并键入/PWD=[PWD] /UID=[UID] /ServerName=[SERVERNAME] /dir="[TARGETDIR]/"

注意各个 用户数据之间是有空格的。

 

最后生成即可

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Configuration.Install;
  5. using System.IO;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.ComponentModel;
  9. using System.Diagnostics;
  10. using System.Reflection;
  11. namespace InstallDB
  12. {      
  13.     [RunInstaller(true)]
  14.     public class InstallDB:Installer
  15.     {
  16.         private void InstallDateBase()
  17.         {
  18.             
  19.             SqlConnection sqlConnection1 = new SqlConnection();
  20.             string DBName = "Spider";// this.Context.Parameters["DataBaseName"];
  21.             string DBServer = this.Context.Parameters["ServerName"];
  22.             string DBUser = this.Context.Parameters["UID"];
  23.             string DBKey = this.Context.Parameters["PWD"];
  24.             string dir = this.Context.Parameters["dir"];
  25.             string CONSTR = @"server=" + DBServer + ";database=master;uid=" + DBUser + ";pwd=" + DBKey;
  26.             StringBuilder sql = new StringBuilder();
  27.             string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  28.             string dbpath = Path.Combine(path, "DataBase");
  29.             using (SqlConnection sqlInstallCon = new SqlConnection(CONSTR))
  30.             {
  31.                 sqlInstallCon.Open(); ;
  32.                 string datafile = System.IO.Path.Combine(dbpath, "Spider.mdf");
  33.                 string logfile = System.IO.Path.Combine(dbpath, "Spider_log.ldf");
  34.                 string MSQL = "exec sp_attach_db N'" + DBName + "',N'" + datafile + "',N'" + logfile + "'";
  35.                 SqlCommand cmd = new SqlCommand(MSQL, sqlInstallCon);
  36.                 cmd.ExecuteNonQuery();
  37.             }
  38.             //
  39.             //修改网页和应用程序的配置文件
  40.             string appFile =path +"//SpiderMain.exe.config";
  41.             string webFile = dir + "web.config";
  42.             string configValue = string.Format("server={0};database={1};uid={2};pwd={3}",DBServer,DBName,DBUser,DBKey);
  43.             AppConfig config = new AppConfig(appFile);
  44.             config.SetValue("ConnectionString", configValue);
  45.             config.docName = webFile;
  46.             config.SetValue("ConnectionString", configValue);
  47.            //安装ajax控件
  48.             ProcessStartInfo psi = new ProcessStartInfo();
  49.             psi.WorkingDirectory = dbpath;
  50.             psi.FileName = @"ASP.NET 2.0 AJAX Extensions 1.0.msi";
  51.             psi.UseShellExecute = true//msi文件,如是exe不用设
  52.             Process.Start(psi);
  53.         }
  54.         public override void Install(System.Collections.IDictionary stateSaver)
  55.         {
  56.             try
  57.             {
  58.                 base.Install(stateSaver);
  59.                 this.InstallDateBase();//调用上面的方法
  60.             }
  61.             catch
  62.             {
  63.                 throw;
  64.             }
  65.         }
  66.         public override void Uninstall(System.Collections.IDictionary stateSaver)
  67.         {
  68.             base.Uninstall(stateSaver);
  69.         }
  70.         public override void Commit(System.Collections.IDictionary stateSaver)
  71.         {
  72.             base.Commit(stateSaver);
  73.         }
  74.         public override void Rollback(System.Collections.IDictionary stateSaver)
  75.         {
  76.             base.Rollback(stateSaver);
  77.         }
  78.     }
  79. }

 

 

  1. using System;
  2. using System.Xml;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Reflection;
  6. using System.Diagnostics;
  7. namespace InstallDB
  8. {
  9.     public class AppConfig : System.Configuration.AppSettingsReader
  10.     {
  11.         public string docName = String.Empty;
  12.         private XmlNode node = null;
  13.         public AppConfig(string file)
  14.         {
  15.             docName = file;
  16.         }
  17.         public bool SetValue(string key, string value)
  18.     {
  19.         XmlDocument cfgDoc = new XmlDocument();
  20.         cfgDoc.Load(docName);
  21.         System.IO.StreamWriter f = new System.IO.StreamWriter("c://34.txt"true);
  22.         f.WriteLine("KKK:" + docName);
  23.         f.WriteLine("key:" +key +"/n" + "value:" + value);
  24.         f.Close();
  25.         // retrieve the appSettings node 
  26.         node = cfgDoc.SelectSingleNode("//appSettings");
  27.         if (node == null)
  28.         {
  29.             throw new System.InvalidOperationException("appSettings section not found");
  30.         }
  31.         try
  32.         {
  33.             // XPath select setting "add" element that contains this key      
  34.             XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
  35.             if (addElem != null)
  36.             {
  37.                 addElem.SetAttribute("value", value);
  38.             }
  39.             // not found, so we need to add the element, key and value
  40.             else
  41.             {
  42.                 XmlElement entry = cfgDoc.CreateElement("add");
  43.                 entry.SetAttribute("key", key);
  44.                 entry.SetAttribute("value", value);
  45.                 node.AppendChild(entry);
  46.             }
  47.             //save it
  48.             saveConfigDoc(cfgDoc, docName);
  49.             return true;
  50.         }
  51.         catch
  52.         {
  53.             return false;
  54.         }
  55.     }
  56.         private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
  57.         {
  58.             try
  59.             {
  60.                 XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
  61.                 writer.Formatting = Formatting.Indented;
  62.                 cfgDoc.WriteTo(writer);
  63.                 writer.Flush();
  64.                 writer.Close();
  65.                 return;
  66.             }
  67.             catch
  68.             {
  69.                 throw;
  70.             }
  71.         }
  72.         public bool removeElement(string elementKey)
  73.         {
  74.             try
  75.             {
  76.                 XmlDocument cfgDoc = new XmlDocument();
  77.                 cfgDoc.Load(docName);
  78.                 // retrieve the appSettings node 
  79.                 node = cfgDoc.SelectSingleNode("//appSettings");
  80.                 if (node == null)
  81.                 {
  82.                     throw new System.InvalidOperationException("appSettings section not found");
  83.                 }
  84.                 // XPath select setting "add" element that contains this key to remove    
  85.                 node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
  86.                 saveConfigDoc(cfgDoc, docName);
  87.                 return true;
  88.             }
  89.             catch
  90.             {
  91.                 return false;
  92.             }
  93.         }
  94.     }
  95. }
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值