最近在做一个项目时,同进涉及WinFrom和Web的安装过程,并且两上项目都有各自配置文件,分别为App.config和Web.config。
导入数据库成为第一件头痛的事,而后又要手动配置文件,非常麻烦。如何能让程序在安装的过程中也同时修改相应的配置文件。
安装部署主要分以下几个流程:
一、创建一个工程,用来对附加数据库和修改配置文件
二、自定义安装部署
一、创建一个类库文件,工程名称为:InstallDB
并且给工程添加System.Configuration.Install和System.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]/"
注意各个 用户数据之间是有空格的。
最后生成即可
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Configuration.Install;
- using System.IO;
- using System.Data;
- using System.Data.SqlClient;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Reflection;
- namespace InstallDB
- {
- [RunInstaller(true)]
- public class InstallDB:Installer
- {
- private void InstallDateBase()
- {
- SqlConnection sqlConnection1 = new SqlConnection();
- string DBName = "Spider";// this.Context.Parameters["DataBaseName"];
- string DBServer = this.Context.Parameters["ServerName"];
- string DBUser = this.Context.Parameters["UID"];
- string DBKey = this.Context.Parameters["PWD"];
- string dir = this.Context.Parameters["dir"];
- string CONSTR = @"server=" + DBServer + ";database=master;uid=" + DBUser + ";pwd=" + DBKey;
- StringBuilder sql = new StringBuilder();
- string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- string dbpath = Path.Combine(path, "DataBase");
- using (SqlConnection sqlInstallCon = new SqlConnection(CONSTR))
- {
- sqlInstallCon.Open(); ;
- string datafile = System.IO.Path.Combine(dbpath, "Spider.mdf");
- string logfile = System.IO.Path.Combine(dbpath, "Spider_log.ldf");
- string MSQL = "exec sp_attach_db N'" + DBName + "',N'" + datafile + "',N'" + logfile + "'";
- SqlCommand cmd = new SqlCommand(MSQL, sqlInstallCon);
- cmd.ExecuteNonQuery();
- }
- //
- //修改网页和应用程序的配置文件
- string appFile =path +"//SpiderMain.exe.config";
- string webFile = dir + "web.config";
- string configValue = string.Format("server={0};database={1};uid={2};pwd={3}",DBServer,DBName,DBUser,DBKey);
- AppConfig config = new AppConfig(appFile);
- config.SetValue("ConnectionString", configValue);
- config.docName = webFile;
- config.SetValue("ConnectionString", configValue);
- //安装ajax控件
- ProcessStartInfo psi = new ProcessStartInfo();
- psi.WorkingDirectory = dbpath;
- psi.FileName = @"ASP.NET 2.0 AJAX Extensions 1.0.msi";
- psi.UseShellExecute = true; //msi文件,如是exe不用设
- Process.Start(psi);
- }
- public override void Install(System.Collections.IDictionary stateSaver)
- {
- try
- {
- base.Install(stateSaver);
- this.InstallDateBase();//调用上面的方法
- }
- catch
- {
- throw;
- }
- }
- public override void Uninstall(System.Collections.IDictionary stateSaver)
- {
- base.Uninstall(stateSaver);
- }
- public override void Commit(System.Collections.IDictionary stateSaver)
- {
- base.Commit(stateSaver);
- }
- public override void Rollback(System.Collections.IDictionary stateSaver)
- {
- base.Rollback(stateSaver);
- }
- }
- }
- using System;
- using System.Xml;
- using System.Configuration;
- using System.Collections;
- using System.Reflection;
- using System.Diagnostics;
- namespace InstallDB
- {
- public class AppConfig : System.Configuration.AppSettingsReader
- {
- public string docName = String.Empty;
- private XmlNode node = null;
- public AppConfig(string file)
- {
- docName = file;
- }
- public bool SetValue(string key, string value)
- {
- XmlDocument cfgDoc = new XmlDocument();
- cfgDoc.Load(docName);
- System.IO.StreamWriter f = new System.IO.StreamWriter("c://34.txt", true);
- f.WriteLine("KKK:" + docName);
- f.WriteLine("key:" +key +"/n" + "value:" + value);
- f.Close();
- // retrieve the appSettings node
- node = cfgDoc.SelectSingleNode("//appSettings");
- if (node == null)
- {
- throw new System.InvalidOperationException("appSettings section not found");
- }
- try
- {
- // XPath select setting "add" element that contains this key
- XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
- if (addElem != null)
- {
- addElem.SetAttribute("value", value);
- }
- // not found, so we need to add the element, key and value
- else
- {
- XmlElement entry = cfgDoc.CreateElement("add");
- entry.SetAttribute("key", key);
- entry.SetAttribute("value", value);
- node.AppendChild(entry);
- }
- //save it
- saveConfigDoc(cfgDoc, docName);
- return true;
- }
- catch
- {
- return false;
- }
- }
- private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
- {
- try
- {
- XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
- writer.Formatting = Formatting.Indented;
- cfgDoc.WriteTo(writer);
- writer.Flush();
- writer.Close();
- return;
- }
- catch
- {
- throw;
- }
- }
- public bool removeElement(string elementKey)
- {
- try
- {
- XmlDocument cfgDoc = new XmlDocument();
- cfgDoc.Load(docName);
- // retrieve the appSettings node
- node = cfgDoc.SelectSingleNode("//appSettings");
- if (node == null)
- {
- throw new System.InvalidOperationException("appSettings section not found");
- }
- // XPath select setting "add" element that contains this key to remove
- node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));
- saveConfigDoc(cfgDoc, docName);
- return true;
- }
- catch
- {
- return false;
- }
- }
- }
- }