C#读写config配置文件的方法
程序员文章站
2022-03-10 14:29:13
如下所示:
如下所示:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appsettings> <add key="serverip" value="127.0.0.1"></add> <add key="database" value="warehousedb"></add> <add key="user" value="sa"></add> <add key="password" value="sa"></add> </appsettings> </configuration>
对config配置文件的读写类
using system; using system.collections.generic; using system.linq; using system.text; using system.text.regularexpressions; using system.configuration; using system.servicemodel; using system.servicemodel.configuration; namespace netutilitylib { public static class confighelper { //依据连接串名字connectionname返回数据连接字符串 public static string getconnectionstringsconfig(string connectionname) { //指定config文件读取 string file = system.windows.forms.application.executablepath; system.configuration.configuration config = configurationmanager.openexeconfiguration(file); string connectionstring = config.connectionstrings.connectionstrings[connectionname].connectionstring.tostring(); return connectionstring; } ///<summary> ///更新连接字符串 ///</summary> ///<param name="newname">连接字符串名称</param> ///<param name="newconstring">连接字符串内容</param> ///<param name="newprovidername">数据提供程序名称</param> public static void updateconnectionstringsconfig(string newname, string newconstring, string newprovidername) { //指定config文件读取 string file = system.windows.forms.application.executablepath; configuration config = configurationmanager.openexeconfiguration(file); bool exist = false; //记录该连接串是否已经存在 //如果要更改的连接串已经存在 if (config.connectionstrings.connectionstrings[newname] != null) { exist = true; } // 如果连接串已存在,首先删除它 if (exist) { config.connectionstrings.connectionstrings.remove(newname); } //新建一个连接字符串实例 connectionstringsettings mysettings = new connectionstringsettings(newname, newconstring, newprovidername); // 将新的连接串添加到配置文件中. config.connectionstrings.connectionstrings.add(mysettings); // 保存对配置文件所作的更改 config.save(configurationsavemode.modified); // 强制重新载入配置文件的connectionstrings配置节 configurationmanager.refreshsection("connectionstrings"); } ///<summary> ///返回*.exe.config文件中appsettings配置节的value项 ///</summary> ///<param name="strkey"></param> ///<returns></returns> public static string getappconfig(string strkey) { string file = system.windows.forms.application.executablepath; configuration config = configurationmanager.openexeconfiguration(file); foreach (string key in config.appsettings.settings.allkeys) { if (key == strkey) { return config.appsettings.settings[strkey].value.tostring(); } } return null; } ///<summary> ///在*.exe.config文件中appsettings配置节增加一对键值对 ///</summary> ///<param name="newkey"></param> ///<param name="newvalue"></param> public static void updateappconfig(string newkey, string newvalue) { string file = system.windows.forms.application.executablepath; configuration config = configurationmanager.openexeconfiguration(file); bool exist = false; foreach (string key in config.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地址 public static void updateservicemodelconfig(string configpath, string serverip) { configuration config = configurationmanager.openexeconfiguration(configpath); configurationsectiongroup sec = config.sectiongroups["system.servicemodel"]; servicemodelsectiongroup servicemodelsectiongroup = sec as servicemodelsectiongroup; clientsection clientsection = servicemodelsectiongroup.client; foreach (channelendpointelement item in clientsection.endpoints) { string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; string address = item.address.tostring(); string replacement = string.format("{0}", serverip); address = regex.replace(address, pattern, replacement); item.address = new uri(address); } config.save(configurationsavemode.modified); configurationmanager.refreshsection("system.servicemodel"); } // 修改applicationsettings中app.properties.settings中服务的ip地址 public static void updateconfig(string configpath, string serverip) { configuration config = configurationmanager.openexeconfiguration(configpath); configurationsectiongroup sec = config.sectiongroups["applicationsettings"]; configurationsection configsection = sec.sections["dataservice.properties.settings"]; clientsettingssection clientsettingssection = configsection as clientsettingssection; if (clientsettingssection != null) { settingelement element1 = clientsettingssection.settings.get("dataservice_systemmanagerws_systemmanagerws"); if (element1 != null) { clientsettingssection.settings.remove(element1); string oldvalue = element1.value.valuexml.innerxml; element1.value.valuexml.innerxml = getnewip(oldvalue, serverip); clientsettingssection.settings.add(element1); } settingelement element2 = clientsettingssection.settings.get("dataservice_equipmanagerws_equipmanagerws"); if (element2 != null) { clientsettingssection.settings.remove(element2); string oldvalue = element2.value.valuexml.innerxml; element2.value.valuexml.innerxml = getnewip(oldvalue, serverip); clientsettingssection.settings.add(element2); } } config.save(configurationsavemode.modified); configurationmanager.refreshsection("applicationsettings"); } private static string getnewip(string oldvalue, string serverip) { string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; string replacement = string.format("{0}", serverip); string newvalue = regex.replace(oldvalue, pattern, replacement); return newvalue; } } }
测试代码如下:
class program { static void main(string[] args) { try { //string file = system.windows.forms.application.executablepath + ".config"; //string file1 = appdomain.currentdomain.setupinformation.configurationfile; string serverip = confighelper.getappconfig("serverip"); string db = confighelper.getappconfig("database"); string user = confighelper.getappconfig("user"); string password = confighelper.getappconfig("password"); console.writeline(serverip); console.writeline(db); console.writeline(user); console.writeline(password); confighelper.updateappconfig("serverip", "192.168.1.11"); string newip = confighelper.getappconfig("serverip"); console.writeline(newip); console.readkey(); } catch (exception ex) { console.writeline(ex.message); } } }
以上这篇c#读写config配置文件的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇: Java负载均衡服务器实现上传文件同步