-
using System;
-
using System.Collections;
-
using System.ComponentModel;
-
using System.Configuration.Install;
-
using System.IO;
-
using System.Linq;
-
using System.Reflection;
-
using System.Xml.Linq;
-
using System.Diagnostics;
-
using System.Windows.Forms;
-
-
namespace Freemansoft.Csm
-
{
-
/// <summary>
-
/// 自定义安装。
-
/// </summary>
-
[RunInstaller(true)]
-
public partial class CsmInstaller : Installer
-
{
-
/// <summary>
-
/// 构造方法。
-
/// </summary>
-
public CsmInstaller()
-
{
-
InitializeComponent();
-
}
-
-
/// <summary>
-
/// 重载提交。
-
/// </summary>
-
public override void Commit(IDictionary savedState)
-
{
-
base.Commit(savedState);
-
-
CreateUnInstallBat();
-
CreateMySQLProviderSection();
-
}
-
-
/// <summary>
-
/// 创建“MySQL数据库”数据提供程序配置段。
-
/// </summary>
-
private static void CreateMySQLProviderSection()
-
{
-
string invarientValue = "MySql.Data.MySqlClient";
-
string machineCfgFileName = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config";
-
string name = "MySQL Data Provider";
-
string description = ".Net Framework Data Provider for MySQL";
-
string type = "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d";
-
-
XDocument document = XDocument.Load(machineCfgFileName);
-
if (document != null)
-
{
-
XElement element = document.Root.Element("system.data");
-
if (element != null)
-
{
-
element = element.Element("DbProviderFactories");
-
if (element != null)
-
{
-
if (element.Elements().FirstOrDefault(a =>
-
a.Attribute("invariant").Value == invarientValue) == null)
-
{
-
element.Add(new XElement("add",
-
new XAttribute("name", name),
-
new XAttribute("invariant", invarientValue),
-
new XAttribute("description", description),
-
new XAttribute("type", type)));
-
-
document.Save(machineCfgFileName);
-
Logging.Logger.Inform("Add the mysql data provider configuration to the " + machineCfgFileName);
-
}
-
}
-
}
-
}
-
}
-
-
/// <summary>
-
/// 创建卸载批处理文件。
-
/// </summary>
-
protected void CreateUnInstallBat()
-
{
-
string dir = GetTargetDirectory();
-
-
FileStream fs = new FileStream(dir + "UnInstall.bat", FileMode.Create);
-
StreamWriter sw = new StreamWriter(fs);
-
sw.WriteLine("@echo off");
-
sw.WriteLine(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u " + Path.Combine(GetTargetDirectory(), "DbSynchronization.exe"));
-
sw.WriteLine(string.Format("start /normal %windir%\\system32\\msiexec /x {0}", base.Context.Parameters["productCode"].ToString()));
-
-
sw.WriteLine("exit");
-
sw.Flush();
-
sw.Close();
-
fs.Close();
-
}
-
-
/// <summary>
-
/// 获取安装目标目录。
-
/// </summary>
-
protected string GetTargetDirectory()
-
{
-
string directory = Path.GetDirectoryName(base.Context.Parameters["assemblypath"].ToString());
-
if (directory[directory.Length - 1] != Path.DirectorySeparatorChar)
-
{
-
directory += Path.DirectorySeparatorChar;
-
}
-
return directory;
-
}
-
}
-
}
-