private void Init() { // 合并kiss.local.config和kiss.config string rootpath = AppDomain.CurrentDomain.BaseDirectory; if (HttpContext.Current != null) { rootpath = Path.Combine(rootpath, "App_Data"); } if (File.Exists(Path.Combine(rootpath, "kiss.config"))) { using (XmlTransformableDocument x = new XmlTransformableDocument()) { x.Load(Path.Combine(rootpath, "kiss.config")); string localfile = FileUtil.FormatDirectory(XmlUtil.GetStringAttribute(x.DocumentElement, "local", HttpContext.Current == null ? ".kiss.local.config" : Path.Combine(".App_Data", "kiss.local.config"))); if (File.Exists(localfile)) { using (XmlTransformation t = new XmlTransformation(localfile)) { t.Apply(x); } } root = x.DocumentElement; _EmptyNode = x.CreateElement("__empty__"); } } else { XmlDocument xml = new XmlDocument(); root = xml; _EmptyNode = xml.CreateElement("__empty__"); } // 合并数据库连接字符串 XmlNode node_conn = root.SelectSingleNode("connectionStrings"); if (node_conn != null && node_conn.ChildNodes.Count > 0) { FieldInfo fi = typeof(ConfigurationElementCollection) .GetField("bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic); if (fi != null) { fi.SetValue(ConfigurationManager.ConnectionStrings, false); foreach (XmlNode item in node_conn.ChildNodes) { if (item.Name == "clear") { ConfigurationManager.ConnectionStrings.Clear(); } else if (item.Name == "add") { string name = XmlUtil.GetStringAttribute(item, "name", null); if (string.IsNullOrEmpty(name)) { continue; } string conn = XmlUtil.GetStringAttribute(item, "connectionString", null); if (string.IsNullOrEmpty(name)) { continue; } string provider = XmlUtil.GetStringAttribute(item, "providerName", null); if (string.IsNullOrEmpty(provider)) { continue; } if (ConfigurationManager.ConnectionStrings[name] != null) { ConfigurationManager.ConnectionStrings.Remove(name); } ConfigurationManager.ConnectionStrings.Add(new ConnectionStringSettings(name, conn, provider)); } } fi.SetValue(ConfigurationManager.ConnectionStrings, true); } else { LogManager.GetLogger <Configuration>().Warn("Can't modify ConfigurationManager.ConnectionStrings."); } } }