Winform—C#读写config配置文件
2017-07-13 15:35
501 查看
现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.
一、如何使用ConfigurationManager?
1、添加引用:添加System.configguration
修改IP地址
config 读写方法
实际应用:
1、获取配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击获取配置节<connectionStrings>指定name的connectionString值
结果为:
2、修改配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击修改配置节<connectionStrings>指定key的value值为文本框的值
button3 点击获取配置节<appSettings>指定key新的value值
结果为:
此时配置文件key1的value值为,获取key值仍为修改前的值
如何重置为修改前的值?
如何保存修改后的值?
一、如何使用ConfigurationManager?
1、添加引用:添加System.configguration
// 修改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; }
修改IP地址
config 读写方法
using System.Configuration; //省略其他代码 public SalesOrderData() { string str = ""; str = ConfigurationManager.ConnectionStrings["kyd"].ToString(); conn = new SqlConnection(str); cmd = conn.CreateCommand(); }
实际应用:
1、获取配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击获取配置节<connectionStrings>指定name的connectionString值
结果为:
2、修改配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击修改配置节<connectionStrings>指定key的value值为文本框的值
button3 点击获取配置节<appSettings>指定key新的value值
结果为:
此时配置文件key1的value值为,获取key值仍为修改前的值
如何重置为修改前的值?
如何保存修改后的值?
相关文章推荐
- c# winform app.config xml 配置文件 读写操作
- C#中动态读写App.config配置文件 - zyloveyrf jinghuazhi.com
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- C#读写配置文件(.config)简介
- C#读写配置文件(.config)(二)
- c#,WinForm中读写配置文件App.config
- C#读写App.config配置文件
- C#_WinForm程序的App.Config数据库连接配置文件
- C#读写config配置文件
- C# App.config文件配置数据的读写
- C#读写config配置文件
- c#,WinForm中读写配置文件App.config
- c#,WinForm中读写配置文件App.config
- c#,WinForm中读写配置文件App.config
- C# 读写App.config配置文件的方法
- C#WinForm APP.CONFIG配置文件的操作
- C#读写Config配置文件
- C# WinForm程序的App.Config数据库连接配置文件
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- 【转】C# WINFORM 应用程序动态读写xml config文件,获取数据库连接并对App.config文件进行加密