private string _getConfigConnectionSetting(string configPath) { XmlDocument xmlDocument = new XmlDocument(); XmlNode targetNode = null; using (var fs = new FileStream(configPath, FileMode.Open)) { xmlDocument.Load(fs); XmlNodeList nodes = xmlDocument.SelectNodes("/configuration/connectionStrings/add"); foreach (XmlNode node in nodes) { if (node.Attributes["name"].Value.ToLower() == ConfigValue.ToLower()) { targetNode = node; } } } if (targetNode != null) { return(targetNode.Attributes["connectionString"].Value); } throw new ApplicationException(string.Format("Could not find the specified app setting of '{0}' in {1}", ConfigValue, configPath)); }
private void _setProtectedConfigSection(string configPath) { XmlDocument xmlDocument = new XmlDocument(); using (var fs = new FileStream(configPath, FileMode.Open)) { xmlDocument.Load(fs); } XmlNodeList nodes = xmlDocument.SelectNodes("/configuration/ProtectedConfigSection/appSettings/add"); XmlNode targetNode = null; foreach (XmlNode node in nodes) { if (node.Attributes["key"].Value.ToLower() == ConfigValue.ToLower()) { targetNode = node; } } if (targetNode != null) { targetNode.Attributes["value"].Value = TargetValue == null ? string.Empty : TargetValue; using (FileStream fs = new FileStream(configPath, FileMode.Create, FileAccess.Write)) { xmlDocument.Save(fs); } } else { throw new ApplicationException(string.Format("Could not find the specified app setting of '{0}' in {1}", ConfigValue, configPath)); } }
private string _getProtectedConfigSection(string configPath) { XmlDocument xmlDocument = new XmlDocument(); using (var fs = new FileStream(configPath, FileMode.Open)) { xmlDocument.Load(fs); } XmlNodeList nodes = xmlDocument.SelectNodes("/configuration/ProtectedConfigSection/appSettings/add"); foreach (XmlNode node in nodes) { if (node.Attributes["key"].Value.ToLower() == ConfigValue.ToLower()) { return(node.Attributes["value"].Value); } } return(string.Format("Could not find '{0}' AppSetting in {1}", ConfigValue, configPath)); }
private void _setConfigAppSetting(string configPath) { //ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap(); //configFileMap.ExeConfigFilename = configPath; //Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); //config.AppSettings.Settings[ConfigValue].Value = TargetValue; //config.Save(); XmlDocument xmlDocument = new XmlDocument(); using (FileStream fs = new FileStream(configPath, FileMode.Open)) { xmlDocument.Load(fs); } XmlNodeList nodes = xmlDocument.SelectNodes("/configuration/appSettings/add"); XmlNode targetNode = null; foreach (XmlNode node in nodes) { if (node.Attributes["key"].Value.ToLower() == ConfigValue.ToLower()) { targetNode = node; } } if (targetNode != null) { targetNode.Attributes["value"].Value = TargetValue == null ? string.Empty : TargetValue; using (FileStream fs = new FileStream(configPath, FileMode.Create, FileAccess.ReadWrite)) { xmlDocument.Save(fs); } } else { throw new ApplicationException(string.Format("Could not find the specified app setting of '{0}' in {1}", ConfigValue, configPath)); } }