现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.
一、如何使用ConfigurationManager?
1、添加引用:添加System.configguration

2、引用空间
3、config配置文件配置节
常用配置节:
(1)普通配置节
<appSettings>
<add key="COM1" value="COM1,9600,8,None,1,已启用" />
</appSettings>
(2)数据源配置节
<connectionStrings>
<add name="kyd" connectionString="server=.;database=UFDATA_999_2017;user=sa;pwd=123"/>
</connectionStrings>
(3)自定义配置节
二、config文件读写
1、依据连接串名字connectionName返回数据连接字符串
//依据连接串名字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;
}
2、更新连接字符串
///<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);
</span><span style="color: rgba(0, 0, 255, 1)">bool</span> exist = <span style="color: rgba(0, 0, 255, 1)">false</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">记录该连接串是否已经存在
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果要更改的连接串已经存在 </span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (config.ConnectionStrings.ConnectionStrings[newName] != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
exist </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果连接串已存在,首先删除它 </span>
<span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (exist)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">新建一个连接字符串实例 </span>
ConnectionStringSettings mySettings =
<span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ConnectionStringSettings(newName, newConString, newProviderName);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将新的连接串添加到配置文件中. </span>
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection(“connectionStrings”);
}
3、返回*.exe.config文件中appSettings配置节的value项
///<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;
}
4、在*.exe.config文件中appSettings配置节增加一对键值对
///<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");
}
5、修改IP地址
// 修改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(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">system.serviceModel</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改applicationSettings中App.Properties.Settings中服务的IP地址</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> UpdateConfig(<span style="color: rgba(0, 0, 255, 1)">string</span> configPath, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serverIP)
{
Configuration config </span>=<span style="color: rgba(0, 0, 0, 1)"> ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroup sec </span>= config.SectionGroups[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">applicationSettings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
ConfigurationSection configSection </span>= sec.Sections[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService.Properties.Settings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
ClientSettingsSection clientSettingsSection </span>= configSection <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> ClientSettingsSection;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (clientSettingsSection != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
SettingElement element1 </span>= clientSettingsSection.Settings.Get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService_SystemManagerWS_SystemManagerWS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (element1 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
clientSettingsSection.Settings.Remove(element1);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> oldValue =<span style="color: rgba(0, 0, 0, 1)"> element1.Value.ValueXml.InnerXml;
element1.Value.ValueXml.InnerXml </span>=<span style="color: rgba(0, 0, 0, 1)"> GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element1);
}
SettingElement element2 </span>= clientSettingsSection.Settings.Get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService_EquipManagerWS_EquipManagerWS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (element2 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
clientSettingsSection.Settings.Remove(element2);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> oldValue =<span style="color: rgba(0, 0, 0, 1)"> element2.Value.ValueXml.InnerXml;
element2.Value.ValueXml.InnerXml </span>=<span style="color: rgba(0, 0, 0, 1)"> GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element2);
}
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">applicationSettings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">string</span> GetNewIP(<span style="color: rgba(0, 0, 255, 1)">string</span> oldValue, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serverIP)
{
</span><span style="color: rgba(0, 0, 255, 1)">string</span> pattern = <span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">string</span> replacement = <span style="color: rgba(0, 0, 255, 1)">string</span>.Format(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{0}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, serverIP);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> newvalue =<span style="color: rgba(0, 0, 0, 1)"> Regex.Replace(oldValue, pattern, replacement);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> newvalue;
}</span></pre>
修改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值仍为修改前的值
如何重置为修改前的值?
如何保存修改后的值?