.NET配置文件读写实例(附SosoftConfigHelper类)
2012-09-26 22:27
399 查看
配置文件在软件开发中起到举足轻重的作用,可以说不可或缺。.NET程序可使用.config文件作为配置文件,例如WinForm程序的*.app.config、Web程序的web.config。.config文件是标准的XML文件。本实例可读取、修改和添加app.confing或者web.config文件的appSettings。SosoftConfigHelper类还可以读写ConnectionStrings。
使用Visual Studio创建一个WinForm项目,在窗体上建立控件,如图:
键值列表中的值是运行结果。
然后在更新配置按钮事件方法中加入如下代码:
窗体的代码如下:
记得要添加System.Configuration命名空间和程序集的引用。
另外新建一个类,命名SosoftConfigHelper.cs,这是配置文件读写类,代码如下:
最后右击项目选择“添加-新建项”,然后选择“应用程序配置文件”,点击添加按钮就创建配置文件app.config。
app.config的格式如下:
按F5运行,可以添加、修改appSettings配置项和列出所有appSettings配置项。
柔城配置文件读写实例源代码下载地址:http://files.cnblogs.com/sosoft/SosoftConfig.rar
使用Visual Studio创建一个WinForm项目,在窗体上建立控件,如图:
键值列表中的值是运行结果。
然后在更新配置按钮事件方法中加入如下代码:
SosoftConfigHelper.UpdateAppConfig(textBox_key.Text, textBox_value.Text);
窗体的代码如下:
/* Copyright (c) 2012 柔城 All rights reserved. * * sosoft.cnblogs.com */ using System; using System.Configuration; using System.Windows.Forms; using Sosoft.Cnblogs.Com.Helper; namespace Sosoft.Cnblogs.Com { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button_update_Click(object sender, EventArgs e) { SosoftConfigHelper.UpdateAppConfig(textBox_key.Text, textBox_value.Text); GetKeyValueList(); } private void GetKeyValueList() { textBox_keyValueList.Text = string.Empty; foreach (string key in ConfigurationManager.AppSettings) { textBox_keyValueList.Text += key + " : " + SosoftConfigHelper.GetAppConfig(key) + "\r\n"; } } private void MainForm_Load(object sender, EventArgs e) { GetKeyValueList(); } } }
记得要添加System.Configuration命名空间和程序集的引用。
另外新建一个类,命名SosoftConfigHelper.cs,这是配置文件读写类,代码如下:
/* Copyright (c) 2012 柔城 All rights reserved. * * sosoft.cnblogs.com */ using System; using System.Configuration; namespace Sosoft.Cnblogs.Com.Helper { /// <summary> /// Sosoft配置文件辅助类 /// </summary> public class SosoftConfigHelper { /// <summary> /// 读取appStrings配置节, 返回*.exe.config文件中appSettings配置节的value项 /// </summary> /// <param name="strKey"></param> /// <returns></returns> public static string GetAppConfig(string strKey) { foreach (string key in ConfigurationManager.AppSettings) { if (key == strKey) { return ConfigurationManager.AppSettings[strKey]; } } return null; } /// <summary> /// 更新appStrings配置节,在*.exe.config文件中appSettings配置节增加一对键、值对 /// </summary> /// <param name="newKey"></param> /// <param name="newValue"></param> public static void UpdateAppConfig(string newKey, string newValue) { bool isModified = false; foreach (string key in ConfigurationManager.AppSettings) { if (key == newKey) { isModified = true; } } // Open App.Config of executable Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // You need to remove the old settings object before you can replace it if (isModified) { config.AppSettings.Settings.Remove(newKey); } // Add an Application Setting. config.AppSettings.Settings.Add(newKey, newValue); // Save the changes in App.config file. config.Save(ConfigurationSaveMode.Modified); // Force a reload of a changed section. ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 读取connectionStrings配置节,依据连接串名字connectionName返回数据连接字符串 /// </summary> /// <param name="connectionName"></param> /// <returns></returns> public static string GetConnectionStringsConfig(string connectionName) { string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString.ToString(); Console.WriteLine(connectionString); return connectionString; } /// <summary> /// 更新connectionStrings配置节, 更新连接字符串 /// </summary> /// <param name="newName"> 连接字符串名称 </param> /// <param name="newConString"> 连接字符串内容 </param> /// <param name="newProviderName"> 数据提供程序名称 </param> public static void UpdateConnectionStringsConfig(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"); } } }
最后右击项目选择“添加-新建项”,然后选择“应用程序配置文件”,点击添加按钮就创建配置文件app.config。
app.config的格式如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="SosoftKey" value="sosoftValue" /> <add key="SosoftURL" value="sosoft.cnblogs.com" /> <add key="SosoftProject" value="sosoft.codeplex.com" /> </appSettings> </configuration>
按F5运行,可以添加、修改appSettings配置项和列出所有appSettings配置项。
柔城配置文件读写实例源代码下载地址:http://files.cnblogs.com/sosoft/SosoftConfig.rar
相关文章推荐
- .net读写配置文件
- Quartz.net配置文件实例及cron表达式详解
- .net下读写配置文件app.config方法
- .net读写配置文件
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- .Net配置文件——反射+配置文件存储类型实例
- Python 读写一个配置文件的实例分解 (一)
- QSetting:Qt读写配置文件实例
- .Net配置文件——反射+配置文件存储类型实例
- .NET编程 -- .NET 2.0 中对配置文件的读写
- .net 2.0 中对配置文件的读写
- .net 2.0 中对配置文件的读写(ConfigurationManager)
- .Net2.0使用ConfigurationManager读写配置文件
- python读写ini配置文件方法实例分析
- .NET平台开源项目速览(1)SharpConfig配置文件读写组件
- .Net2.0 使用ConfigurationManager读写配置文件
- Quartz.net配置文件实例及cron表达式详解
- .Net配置文件——反射+配置文件存储类型实例
- .NET 2.0中,配置文件app.config的读写(VS2005,C#)
- [导入].NET 2.0中,配置文件app.config的读写(VS2005,C#)