User Id :    Password :      New Member   Forgot Password  
 
How can we write in App.config file at run time?
Description We can write in App.config file at run time.   No. of Views     2692
  Rating     5
Author Ankur Gupta   Posted On     13 Jul 2010
Tags ASP.NET    

Sample Code   Download Code

How can we write in App.config file at run time?

Generally we use App.config file in project to save the database connection string or any other Valuable values which we want to use in entire project.

But if we write config file manually then user can see all information after open this. So It is helpful to hide all information in config file If we write this at run time.

First save data in Temporary format in file so that the Existing Open application can use the saved values.
public static void SaveData(string ConnectionString1)
        {
            System.Configuration.Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings.Remove("SqlConnectionADODB");
            config.AppSettings.Settings.Add("SqlConnectionADODB", ConnectionString1);
            config.Save();
            ConfigurationManager.RefreshSection("appSettings");
        } 

After that you can Save data permamently In App.config file.
  public static void WriteData(string key, string value)
        {
            ////load config document for current assembly
            XmlDocument doc = loadConfigDocument();
            // retrieve appSettings node
            XmlNode node = doc.SelectSingleNode("//appSettings");
            if (node == null)
            {
                throw new InvalidOperationException("appSettings section not found in config file.");
            }
            try
            {
                // select the 'add' element that contains the key
                XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                if (elem != null)
                {
                    // add value for key
                    elem.SetAttribute("value", value);
                }
                else
                {
                    // key was not found so create the 'add' element
                    // and set it's key/value attributes
                    elem = doc.CreateElement("add");
                    elem.SetAttribute("key", key);
                    elem.SetAttribute("value", value);
                    node.AppendChild(elem);
                }
                doc.Save(getConfigFilePath());
            }
            catch
            {
                throw;
            }
        }
        public static XmlDocument loadConfigDocument()
        {
            XmlDocument doc = null;
            try
            {
                doc = new XmlDocument();
                doc.Load(getConfigFilePath());
                return doc;
            }
            catch (System.IO.FileNotFoundException e)
            {
                throw new Exception("No configuration file found.", e);
            }
        }
        public static string getConfigFilePath()
        {
            return Assembly.GetExecutingAssembly().Location + ".config";
        }

You should use both function SaveData and WriteData simultaneously in your application.

About Author

About Author I am Ankur Gupta Working in MNC as a sr. software engg. My major area of experiance in Accounting and CRM with C#,VB.Net,WCF,ASP.net languages. Ankur
No Photo
 
Country N/A
Company PILTD
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code