ARTICLE

Write in App.config file

Posted by Ankur Gupta Articles | Active Directory in VB.NET May 26, 2010
How can we write app.config file.
Download Files:
 
Reader Level:

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.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi dear I need to add refrence to dll file in vb.net 2008 by code - programaticaly thanks alot for your kind help

Posted by msh msh Jan 25, 2012

You can add refrence of System.Configuration and System.Xml

Posted by Ankur Gupta May 26, 2010

there are need for adding some reference(s) or not??

Posted by Hirendra Sisodiya May 26, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor