如何读取多个config配置文件

如题所述

代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Configuration;usingSystem.ServiceModel;usingSystem.ServiceModel.Configuration;namespaceNetUtilityLib{publicstaticclassConfigHelper{//依据连接串名字connectionName返回数据连接字符串publicstaticstringGetConnectionStringsConfig(stringconnectionName){//指定config文件读取stringfile=System.Windows.Forms.Application.ExecutablePath;System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);stringconnectionString=config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString();returnconnectionString;}//////更新连接字符串//////连接字符串名称///连接字符串内容///数据提供程序名称publicstaticvoidUpdateConnectionStringsConfig(stringnewName,stringnewConString,stringnewProviderName){//指定config文件读取stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);boolexist=false;//记录该连接串是否已经存在//如果要更改的连接串已经存在if(config.ConnectionStrings.ConnectionStrings[newName]!=null){exist=true;}//如果连接串已存在,首先删除它if(exist){config.ConnectionStrings.ConnectionStrings.Remove(newName);}//新建一个连接字符串实例ConnectionStringSettingsmySettings=newConnectionStringSettings(newName,newConString,newProviderName);//将新的连接串添加到配置文件中.config.ConnectionStrings.ConnectionStrings.Add(mySettings);//保存对配置文件所作的更改config.Save(ConfigurationSaveMode.Modified);//强制重新载入配置文件的ConnectionStrings配置节ConfigurationManager.RefreshSection("ConnectionStrings");}//////返回*.exe.config文件中appSettings配置节的value项/////////publicstaticstringGetAppConfig(stringstrKey){stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);foreach(stringkeyinconfig.AppSettings.Settings.AllKeys){if(key==strKey){returnconfig.AppSettings.Settings[strKey].Value.ToString();}}returnnull;}//////在*.exe.config文件中appSettings配置节增加一对键值对/////////publicstaticvoidUpdateAppConfig(stringnewKey,stringnewValue){stringfile=System.Windows.Forms.Application.ExecutablePath;Configurationconfig=ConfigurationManager.OpenExeConfiguration(file);boolexist=false;foreach(stringkeyinconfig.AppSettings.Settings.AllKeys){if(key==newKey){exist=true;}}if(exist){config.AppSettings.Settings.Remove(newKey);}config.AppSettings.Settings.Add(newKey,newValue);config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("appSettings");}//修改system.serviceModel下所有服务终结点的IP地址publicstaticvoidUpdateServiceModelConfig(stringconfigPath,stringserverIP){Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);ConfigurationSectionGroupsec=config.SectionGroups["system.serviceModel"];ServiceModelSectionGroupserviceModelSectionGroup=secasServiceModelSectionGroup;ClientSectionclientSection=serviceModelSectionGroup.Client;foreach(ChannelEndpointElementiteminclientSection.Endpoints){stringpattern=@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";stringaddress=item.Address.ToString();stringreplacement=string.Format("{0}",serverIP);address=Regex.Replace(address,pattern,replacement);item.Address=newUri(address);}config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("system.serviceModel");}//修改applicationSettings中App.Properties.Settings中服务的IP地址publicstaticvoidUpdateConfig(stringconfigPath,stringserverIP){Configurationconfig=ConfigurationManager.OpenExeConfiguration(configPath);ConfigurationSectionGroupsec=config.SectionGroups["applicationSettings"];ConfigurationSectionconfigSection=sec.Sections["DataService.Properties.Settings"];ClientSettingsSectionclientSettingsSection=configSectionasClientSettingsSection;if(clientSettingsSection!=null){SettingElementelement1=clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS");if(element1!=null){clientSettingsSection.Settings.Remove(element1);stringoldValue=element1.Value.ValueXml.InnerXml;element1.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);clientSettingsSection.Settings.Add(element1);}SettingElementelement2=clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS");if(element2!=null){clientSettingsSection.Settings.Remove(element2);stringoldValue=element2.Value.ValueXml.InnerXml;element2.Value.ValueXml.InnerXml=GetNewIP(oldValue,serverIP);clientSettingsSection.Settings.Add(element2);}}config.Save(ConfigurationSaveMode.Modified);ConfigurationManager.RefreshSection("applicationSettings");}privatestaticstringGetNewIP(stringoldValue,stringserverIP){stringpattern=@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";stringreplacement=string.Format("{0}",serverIP);stringnewvalue=Regex.Replace(oldValue,pattern,replacement);returnnewvalue;}}}
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜