Click here to Skip to main content
15,901,284 members
Articles / Programming Languages / C#
Article

Manage .NET configuration file appsettings section

Rate me:
Please Sign up or sign in to vote.
1.80/5 (7 votes)
19 Oct 20062 min read 38.8K   694   10   3
This is .NET 2.0 Windows Form Application wich can help you to none tech user to update you appsettings in your application configuration file.

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> 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Telvent Farradyne Inc.
United States United States
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

Comments and Discussions

 
GeneralMy vote of 1 Pin
NoCodeMonkey16-Aug-09 15:23
NoCodeMonkey16-Aug-09 15:23 
Poor
Generalunreadable .... Pin
OscarTV14-Mar-07 4:29
OscarTV14-Mar-07 4:29 
GeneralRe: unreadable .... Pin
Haiyan Du14-Mar-07 10:35
Haiyan Du14-Mar-07 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.