如何读取多个config配置文件

 我来答
匿名用户
2017-09-17
展开全部
代码如下: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;}}}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式