您的位置:首页 > 编程语言 > C#

Winform—C#读写config配置文件

2017-07-13 15:35 501 查看
现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.

一、如何使用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值仍为修改前的值



如何重置为修改前的值?



如何保存修改后的值?

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: