配置文件的写入和写出封装
作者:互联网
1.AppConfig配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="DbTypeName" value="SQLServer,Oracle,MySQL,DB2" /> <add key="OracleIP" value="192.168.1.1|Oracle" /> <add key="OracleName" value="name1|Oracle" /> <add key="OraclePwd" value="pwd1|Oracle" /> <add key="OracleLink" value="哈哈|嘿嘿" /> <add key="SqlServerIP" value="172.16.1.185|192.168.1.1|SqlServer" /> <add key="SqlServerName" value="sa|SqlServer|name2" /> <add key="SqlServerPwd" value="~!*%ForDevelop)20|pwd1|SqlServer" /> <add key="SqlServerLink" value="Data Source=172.16.1.185;Initial Catalog=;User=sa;PassWord=~!*%ForDevelop)20;Connect Timeout=50|172.16.1.185|哈哈|嘿嘿" /> <add key="MySqlIP" value="MySqlIP|192.168.1.2" /> <add key="MySqlName" value="MySql|name2" /> <add key="MySqlPwd" value="MySql|pwd2" /> <add key="MySqlLink" value="哈哈|嘿嘿" /> <add key="DB2IP" value="DB2IP|192.168.1.2" /> <add key="DB2Name" value="DB2IP|name2" /> <add key="DB2Pwd" value="DB2IP|pwd2" /> </appSettings> <connectionStrings> <!--链接字符串 模板--> <add name="OracleLinkStr" connectionString="Data Source = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ¥IP)(PORT = 1521))(CONNECT_DATA = (SERVICE_NAME = ¥ORCL))); Persist Security Info = True; User ID = ¥Name; Password = ¥PWD;" /> <add name="SqlServerLinkStr" connectionString="Data Source=¥IP;Initial Catalog=;User=¥Name;PassWord=¥PWD;Connect Timeout=50" /> <add name="MySqlLinkStr" connectionString="" /> </connectionStrings> </configuration>
1、读取:
ConfigurationManager.AppSettings["节点名称"]?.ToString()?.Trim(); ConfigurationManager.ConnectionStrings["节点名称"]?.ToString().Trim();
2、写入封装AppSetting:
/// <summary> /// 写入 /// </summary> /// <param name="appSettingNodeName"></param> private void SetConfigFileHelper(string appSettingNodeName, string value) { //最大保存数量 int maxCount = 20; List<string> ipListSave = new List<string>(); string ipStrAll = ConfigurationManager.AppSettings[appSettingNodeName].ToString().Trim(); List<string> ipList = ipStrAll.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries).ToList(); if (ipList.Count > maxCount) { if (!string.IsNullOrWhiteSpace(value)) { ipListSave.Add(value); for (int i = 0; i < maxCount - 1; i++) { ipListSave.Add(ipList[i]); } } string ipStrValue = string.Join("|", ipListSave); SaveAppSetting(appSettingNodeName, ipStrValue); } else { if (!string.IsNullOrWhiteSpace(value)) { if (!ipList.Contains(value)) { ipStrAll = value + "|" + ipStrAll; SaveAppSetting(appSettingNodeName, ipStrAll); } } } } /// <summary> /// 向配置文件中添加键值对,有则修改,无则添加 /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SaveAppSetting(string key, string value) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //新增 if (!ConfigurationManager.AppSettings.AllKeys.Contains(key)) { config.AppSettings.Settings.Add(key, value); config.Save(); return; } //修改 else { config.AppSettings.Settings[key].Value = value; config.Save(); } ConfigurationManager.RefreshSection("AppSettings"); }
3、写入connectionStrings封装
/// <summary> /// 向配置文件中添加键值对,有则修改,无则添加[connectionStrings节点] /// </summary> /// <param name="key"></param> /// <param name="value"></param> public static void SetConnectionStrings(string key, string value) { if (!ConfigurationManager.AppSettings.AllKeys.Contains(key)) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //config.ConnectionStrings.CurrentConfiguration.Add(value); config.Save(); //return; } else { Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); cfa.AppSettings.Settings[key].Value = value; cfa.Save(); } } /// <summary> /// 更新连接字符串 /// </summary> /// <param name="newName"></param> /// <param name="newConString"></param> /// <param name="newProviderName"></param> public void SetConnStriConfig(string newName, string newConString, string newProviderName) { bool isModified = false; //记录该连接串是否已经存在 if (ConfigurationManager.ConnectionStrings[newName] != null) { isModified = true; } //新建一个连接字符串实例 ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProviderName); // 打开可执行的配置文件*.exe.config Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // 如果连接串已存在,首先删除它 if (isModified) { config.ConnectionStrings.ConnectionStrings.Remove(newName); // 将新的连接串添加到配置文件中. config.ConnectionStrings.ConnectionStrings.Add(mySettings); // 保存对配置文件所作的更改 config.Save(ConfigurationSaveMode.Modified); // 强制重新载入配置文件的ConnectionStrings配置节 ConfigurationManager.RefreshSection("ConnectionStrings"); } }
标签:封装,string,配置文件,ConfigurationManager,写入,value,ConnectionStrings,config 来源: https://www.cnblogs.com/yuanshuo/p/15578930.html