/// <summary> /// 基于web.config模型的AppSettings设置 /// </summary> /// <param name="configPath">配置文件路径,相对或完整路径。</param> /// <param name="key">健</param> /// <param name="Value">健的值</param> /// <returns>成功则为0,失败则返回异常信息。</returns> public static string SetAppSettings(string configPath, string key, string Value) { string configFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configPath); try { System.Configuration.ConfigXmlDocument xmlConfig = new System.Configuration.ConfigXmlDocument(); xmlConfig.Load(configFile); System.Xml.XmlNode node = xmlConfig.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']"); if (node != null) { node.Attributes["value"].Value = Value; } else { XmlElement element = xmlConfig.CreateElement("add"); element.SetAttribute("key", key); element.SetAttribute("value", Value); node = xmlConfig.SelectSingleNode("configuration/appSettings"); node.AppendChild(element); } xmlConfig.Save(configFile); return("0"); } catch (Exception ex) { return(ex.Message); } }
/// <summary> /// 获取基于web.config模型的AppSettings键值设置 /// </summary> /// <param name="configPath">配置文件路径,相对或完整路径。</param> /// <param name="key">健</param> /// <returns>存在则返回相应值,异常出错则为空字符。</returns> public static string GetAppSettings(string configPath, string key) { string configFile = Util.ParseAppPath(configPath); string strRet = ""; try { System.Configuration.ConfigXmlDocument xmlConfig = new System.Configuration.ConfigXmlDocument(); xmlConfig.Load(configFile); System.Xml.XmlNode node = xmlConfig.SelectSingleNode("configuration/appSettings/add[@key='" + key + "']"); if (node != null) { strRet = node.Attributes["value"].Value; } } catch (Exception) { } return(strRet); }
static DataWorkerFactory() { if (factory_config == null || String.IsNullOrWhiteSpace(factory_config.Default_ConnectionString_Name)) { try { ConfigXmlDocument xConfig = new ConfigXmlDocument(); xConfig.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile); System.Xml.XmlNode xNode = xConfig.SelectSingleNode(AspNetConfigurationElementName + EnterpriseLibraryConfigurationElementName); if (xNode != null) { System.Xml.XmlAttribute protectedFlag = xNode.Attributes["configProtectionProvider"]; if (protectedFlag != null) { xNode = System.Configuration.ProtectedConfiguration.Providers[protectedFlag.Value].Decrypt(xNode); if (xNode != null) { xNode = xNode.FirstChild; } } if (xNode != null) { System.Xml.XmlAttribute defaultDB = xNode.Attributes[EnterpriseLibraryDefaultDBAttribute]; if (defaultDB != null) { factory_config = new DataWorkerFactoryConfigSection(); factory_config.Default_ConnectionString_Name = defaultDB.Value; return; } } } } catch { } throw new NullReferenceException("DW Factory Config Section not found in config file."); } }
private string GetConnectionString(string strPathToConfigFile, string strAppSettingsKey) { try { ConfigXmlDocument doc = new ConfigXmlDocument(); doc.Load( strPathToConfigFile ); XmlNode nodeKey = doc.SelectSingleNode("//appSettings/add[@key='" + strAppSettingsKey + "']"); string strEncValue = nodeKey.Attributes["value"].Value.ToString(); string strDecValue = ICICRYPT.Reversible.StrDecode(strEncValue,2026820330,true); // MessageBox.Show(strDecValue); SqlConnection objConn = new SqlConnection(strDecValue); objConn.Open(); objConn.Close(); objConn.Dispose(); return strDecValue; } catch (System.Exception ex) { MessageBox.Show(this, "Error obtaining connection to " + strLocation + " database\n\n\"" + ex.Message + "\"\n" + ex.StackTrace, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); this.Close(); } return null; }
private static ConfigurationSection HandleLegacyConfigurationSection(string exeConfigFilename, ConfigurationSectionGroupPath groupPath, string sectionKey) { var sectionPath = groupPath.GetChildGroupPath(sectionKey).ToString(); foreach (var shredSettingsType in _shredSettingsTypes) { if (LegacyShredConfigSectionAttribute.IsMatchingLegacyShredConfigSectionType(shredSettingsType, sectionPath)) { var xmlDocument = new ConfigXmlDocument(); xmlDocument.Load(exeConfigFilename); var sectionElement = xmlDocument.SelectSingleNode("//" + sectionPath); if (sectionElement != null) { var section = (ShredConfigSection) Activator.CreateInstance(shredSettingsType, true); section.LoadXml(sectionElement.OuterXml); return section; } } } return null; }
private void ModifyAppInsightsConfigFile(bool removeSettings = false) { var configFile = Server.MapPath("~/ApplicationInsights.config"); if (!File.Exists(configFile)) { File.Copy(Server.MapPath("~/DesktopModules/AppInsights/ApplicationInsights.config"), configFile); } // Load the ApplicationInsights.config file var config = new ConfigXmlDocument(); config.Load(configFile); var key = removeSettings ? string.Empty : ModuleSettings.GetValueOrDefault("InstrumentationKey", string.Empty); const string namespaceUri = "http://schemas.microsoft.com/ApplicationInsights/2013/Settings"; var nsmgr = new XmlNamespaceManager(config.NameTable); nsmgr.AddNamespace("nr", namespaceUri); // Change instrumentation key var keyNode = config.SelectSingleNode("//nr:InstrumentationKey", nsmgr); if (keyNode != null) { keyNode.InnerText = key; } // Save the modifications config.Save(configFile); }
private static NameValueCollection LoadHostSettings(string host) { NameValueCollection objCol; System.Configuration.ConfigXmlDocument objDoc = new System.Configuration.ConfigXmlDocument(); try { if (System.IO.File.Exists(General.Environment.Current.GetBinDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetBinDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + host + "']"); if (node == null) { return(new NameValueCollection()); } node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } else if (System.IO.File.Exists(General.Environment.Current.GetAppDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetAppDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + host + "']"); if (node == null) { return(new NameValueCollection()); } node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } else { objCol = (NameValueCollection)null; } } catch { objCol = (NameValueCollection)null; } return(objCol); /* * This is the old code that throws exceptions all the time * try * { * objDoc.Load(General.Environment.Current.GetBinDirectory() + "General.config"); * XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + GetCurrentHost() + "']"); * if (node == null) * return null; * node.Attributes.RemoveAll(); * System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); * objCol = (NameValueCollection)nvsh.Create(null, null, node); * } * catch * { * try * { * objDoc.Load(General.Environment.Current.GetAppDirectory() + "General.config"); * XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + GetCurrentHost() + "']"); * if (node == null) * return null; * node.Attributes.RemoveAll(); * System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); * objCol = (NameValueCollection)nvsh.Create(null, null, node); * } * catch * { * objCol = (NameValueCollection)null; * } * } * return objCol; */ }
private static NameValueCollection LoadHostSettings(string host) { NameValueCollection objCol; System.Configuration.ConfigXmlDocument objDoc = new System.Configuration.ConfigXmlDocument(); try { if (System.IO.File.Exists(General.Environment.Current.GetBinDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetBinDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + host + "']"); if (node == null) return new NameValueCollection(); node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } else if (System.IO.File.Exists(General.Environment.Current.GetAppDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetAppDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + host + "']"); if (node == null) return new NameValueCollection(); node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } else { objCol = (NameValueCollection)null; } } catch { objCol = (NameValueCollection)null; } return objCol; /* This is the old code that throws exceptions all the time try { objDoc.Load(General.Environment.Current.GetBinDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + GetCurrentHost() + "']"); if (node == null) return null; node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } catch { try { objDoc.Load(General.Environment.Current.GetAppDirectory() + "General.config"); XmlNode node = objDoc.SelectSingleNode("//HostSettings" + "[@host='" + GetCurrentHost() + "']"); if (node == null) return null; node.Attributes.RemoveAll(); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, node); } catch { objCol = (NameValueCollection)null; } } return objCol; */ }
private static NameValueCollection LoadGlobalSettings() { NameValueCollection objCol; System.Configuration.ConfigXmlDocument objDoc = new System.Configuration.ConfigXmlDocument(); try { if (System.IO.File.Exists(General.Environment.Current.GetBinDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetBinDirectory() + "General.config"); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, objDoc.SelectSingleNode("General.Configuration/GlobalSettings")); } else if (System.IO.File.Exists(General.Environment.Current.GetAppDirectory() + "General.config")) { objDoc.Load(General.Environment.Current.GetAppDirectory() + "General.config"); System.Configuration.NameValueSectionHandler nvsh = new System.Configuration.NameValueSectionHandler(); objCol = (NameValueCollection)nvsh.Create(null, null, objDoc.SelectSingleNode("General.Configuration/GlobalSettings")); } else { objCol = null; //throw new System.IO.FileNotFoundException("General.config could not be loaded"); } } catch { objCol = null; } return objCol; }
public static bool UpdateConnectionString( ) { Project pjt = Kit.GetProjectByName(Kit.SlnKeel.UIProjectName); if (pjt == null) { Common.ShowInfo("您尚未设置UI主界面项目, 因此无法自动配置连接字符串到你的app.config 或者web.config中!"); return(true); } ProjectItem PI = null; for (int i = 1; i <= pjt.ProjectItems.Count; i++) { ProjectItem pix = pjt.ProjectItems.Item(i); if (pix.Name.ToLower().Contains("app.config") || pix.Name.ToLower().Contains("web.config")) { PI = pix; break; } } if (PI == null) { Common.ShowInfo(string.Format("您设置的主UI项目{0}中未找到app.config 或者web.config,因此无法自动设置链接字符串配置", pjt.Name)); } else { bool needUpdate = true; ConfigXmlDocument cxd = new System.Configuration.ConfigXmlDocument(); cxd.Load(PI.get_FileNames(1)); XmlNode xnx = cxd.SelectSingleNode("descendant::configuration/connectionStrings"); string csss = GetProjectConfigNamespec(GetProjectLangType(GetProjectUI())); string name = ""; if (pjt.Kind == "{E24C65DC-7377-472b-9ABA-BC803B73C61A}") { name = "Keel" + csss + ".ConnectionString"; } else { name = ((string)pjt.Properties.Item("RootNamespace").Value) + csss + ".ConnectionString"; } if (xnx != null) { for (int i = 0; i < xnx.ChildNodes.Count; i++) { if (xnx.ChildNodes[i].Attributes["name"].Value.Contains(name)) { if (xnx.ChildNodes[i].Attributes["providerName"].Value != Kit.SlnKeel.ProviderName) { xnx.ChildNodes[i].Attributes["providerName"].Value = Kit.SlnKeel.ProviderName; } if (xnx.ChildNodes[i].Attributes["connectionString"].Value != Kit.SlnKeel.ConnectString) { xnx.ChildNodes[i].Attributes["connectionString"].Value = Kit.SlnKeel.ConnectString; } cxd.Save(PI.get_FileNames(1)); needUpdate = false; break; } } } if (needUpdate) { XmlNode xn = cxd.CreateNode("element", "add", ""); XmlAttribute xzproviderName = cxd.CreateAttribute("providerName"); xzproviderName.Value = Kit.SlnKeel.ProviderName; XmlAttribute xaconnectionString = cxd.CreateAttribute("connectionString"); xaconnectionString.Value = Kit.SlnKeel.ConnectString; XmlAttribute xaname = cxd.CreateAttribute("name"); xaname.Value = name; xn.Attributes.Append(xaname); xn.Attributes.Append(xaconnectionString); xn.Attributes.Append(xzproviderName); if (xnx == null) { XmlNode xnx1 = cxd.SelectSingleNode("descendant::configuration"); xnx = cxd.CreateElement("connectionStrings"); xnx1.AppendChild(xnx); } xnx.AppendChild(xn); // cxd.SelectSingleNode("descendant::configuration/connectionStrings").InnerXml = xnx.InnerXml; cxd.Save(PI.get_FileNames(1)); //;// = cxd.SelectSingleNode("descendant::configuration/connectionStrings/add[@name='" + appnamespace + ".Properties.Settings.ConnectionString']"); //if (xnl.Attributes["providerName"] != null && xnl.Attributes["connectionString"] != null) //{ //} } Common.ShowInfo(string.Format("您设置的主UI项目{0}中到app.config 或者web.config,已经设置了链接字符串{1}", pjt.Name, name)); } return(true); }