Introduction
This project is designed in Visual Studio 2005 trial version. This is very simple project which show you how to manipulate your appsettings in your application configuration file. You may use this project to develop more sophisticated applications.<o:p>
Here is my story. Recent I finished a project which require our clients, who are none tech person or at least not professional, to setup the configuration appsettings section, such as SMTP, Authenticate User ID, and etc.<o:p>
Since they are none tech, they are afraid to change application settins in application configuration file (I am afraid he is going to mess up my configuration file). I try to find a free GUI to manage those configuration file. Most of them cost hundreds dollars. Since I don't need those fancy functions, so I deicide to write one by myself. simple and easy!<o:p>
Step one open a configuration file and then load a strong typed dataset. Display this DataSet in a DataGridView<o:p>
----------------------------------------<o:p>
string exeFilePath = textBox1.Text.Trim(); //find a configuration file<o:p>
// Map to the application configuration file.
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath;
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);<o:p>
<o:p>
this.myDataSet.Tables[0].Rows.Clear(); //clear strong typed DataTable<o:p>
//load into dataset, then display in the DataGridView<o:p>
string[] keys = config.AppSettings.Settings.AllKeys;<o:p>
// <st1:place w:st="on">Loop to get key/value pairs.
for (int i = 0; i < config.AppSettings.Settings.Count; i++)
{
DataRow dr = this.myDataSet.Tables[0].NewRow();
dr["Key"] = keys[i];
dr["Value"] = config.AppSettings.Settings[keys[i]].Value;
this.myDataSet.Tables[0].Rows.Add(dr);
}<o:p>
this.dataGridView1.DataSource = myDataSet.Tables[0];<o:p>
---------------------------------- <o:p>
Configure your DataGridView, so it can be edit and insert new. You don't need write any code to make your DataGridView work. This is one benefit from .NET 2.0. I like it. <o:p>
Next when user click save, you need save it back to configuration file. <o:p>
------------------ <o:p>
string exeFilePath = textBox1.Text.Trim(); <o:p>
// Map to the application configuration file.
ExeConfigurationFileMap configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = exeFilePath;
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);
config.AppSettings.Settings.Clear();
foreach (DataRow dr in myDataSet.Tables[0].Rows)
{
string newKey = dr["key"].ToString().Trim(); <o:p>
string newValue = dr["value"].ToString().Trim(); <o:p>
config.AppSettings.Settings.Add(newKey, newValue); <o:p>
<o:p>
} <o:p>
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified,true);
<o:p>
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings"); <o:p>
---------------------------- <o:p>
Source code is attached as zip file. I am a lazy man I didn't put a lot of comments in the code. If you any questions, please post it. We may study together. <o:p>
Have fun.<o:p>
<o:p>
Leading .NET developer, System Archetect on GIS, CMS, and Cloud Computing System
skills on:
SQL Server, My SQL, Access, Oracle, and IBM AS400, DB2
ASP.NET, HTML, ASP, Style Sheet and XSLT, XML
, ADO. Net, COM+ and Web Service, Crystal Report, Transaction-SQL, DTS Procedure, Windows Form/WPF,WCF, WF
Programming Language: C#, vb.NET, VBScript, JavaScript