Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 281007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:10:01+00:00 2026-05-12T05:10:01+00:00

i am facing one problem. i want to save settings in app.config file i

  • 0

i am facing one problem.

i want to save settings in app.config file

i wrote separate class and defined section in config file..

but when i run the application. it does not save the given values into config file

here is SettingsClass

public class MySetting:ConfigurationSection
    {
        private static MySetting settings = ConfigurationManager.GetSection("MySetting") as MySetting;

        public override bool IsReadOnly()
        {
            return false;
        }

        public static MySetting Settings
        {
            get
            {
                return settings;
            }
        }


        [ConfigurationProperty("CustomerName")]
        public String CustomerName
        {
            get
            {
                return settings["CustomerName"].ToString();
            }
            set
            {
                settings["CustomerName"] = value;
            }
        }


        [ConfigurationProperty("EmailAddress")]
        public String EmailAddress
        {
            get
            {                
                return settings["EmailAddress"].ToString();
            }
            set
            {
                settings["EmailAddress"] = value;
            }
        }


        public static bool Save()
        {
            try
            {
                System.Configuration.Configuration configFile = Utility.GetConfigFile();
                MySetting mySetting = (MySetting )configFile.Sections["MySetting "];

                if (null != mySetting )
                {
                    mySetting .CustomerName = settings["CustomerName"] as string;
                    mySetting .EmailAddress = settings["EmailAddress"] as string;                    

                    configFile.Save(ConfigurationSaveMode.Full);
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }
    }

and this is the code from where i am saving the information in config file

private void SaveCustomerInfoToConfig(String name, String emailAddress)
        {            

            MySetting .Settings.CustomerName = name;
            MySetting .Settings.EmailAddress = emailAddress
            MySetting .Save();
        }

and this is app.config

<configuration>
  <configSections>
    <section name="MySettings" type="TestApp.MySettings, TestApp"/>
  </configSections> 

  <MySettings CustomerName="" EmailAddress="" />  
</configuration>

can u tell me where is the error.. i tried alot and read from internet. but still unable to save information in config file..

i ran the application by double clicking on exe file also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:10:01+00:00Added an answer on May 12, 2026 at 5:10 am

    According to the MSDN: ConfigurationManager.GetSection Method,

    The ConfigurationManager.GetSection method accesses run-time configuration information that it cannot change. To change the configuration, you use the Configuration.GetSection method on the configuration file that you obtain by using one of the following Open methods:

    • OpenExeConfiguration

    • OpenMachineConfiguration

    • OpenMappedExeConfiguration

    However, if you want to update app.config file, I would read it as an xml document and manipulate it as a normal xml document.

    Please see the following example:
    Note: this sample is just for proof-of-concept. Should not be used in production as it is.

    using System;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace ChangeAppConfig
    {
        class Program
        {
            static void Main(string[] args)
            {
                MyConfigSetting.CustomerName = "MyCustomer";
                MyConfigSetting.EmailAddress = "MyCustomer@Company.com";
                MyConfigSetting.TimeStamp = DateTime.Now;
                MyConfigSetting.Save();
            }
        }
    
        //Note: This is a proof-of-concept sample and 
        //should not be used in production as it is.  
        // For example, this is not thread-safe. 
        public class MyConfigSetting
        {
            private static string _CustomerName;
            public static string CustomerName
            {
                get { return _CustomerName; }
                set
                {
                    _CustomerName = value;
                }
            }
    
            private static string _EmailAddress;
            public static string EmailAddress
            {
                get { return _EmailAddress; }
                set
                {
                    _EmailAddress = value;
                }
            }
    
            private static DateTime _TimeStamp;
            public static DateTime TimeStamp
            {
                get { return _TimeStamp; }
                set
                {
                    _TimeStamp = value;
                }
            }
    
            public static void Save()
            {
                XElement myAppConfigFile = XElement.Load(Utility.GetConfigFileName());
                var mySetting = (from p in myAppConfigFile.Elements("MySettings")
                                select p).FirstOrDefault();
                mySetting.Attribute("CustomerName").Value = CustomerName;
                mySetting.Attribute("EmailAddress").Value = EmailAddress;
                mySetting.Attribute("TimeStamp").Value = TimeStamp.ToString();
    
                myAppConfigFile.Save(Utility.GetConfigFileName());
    
            }
        }
    
        class Utility
        {        
            //Note: This is a proof-of-concept and very naive code. 
            //Shouldn't be used in production as it is. 
            //For example, no null reference checking, no file existence checking and etc. 
            public static string GetConfigFileName()
            {            
                const string STR_Vshostexe = ".vshost.exe";
                string appName = Environment.GetCommandLineArgs()[0];
    
                //In case this is running under debugger. 
                if (appName.EndsWith(STR_Vshostexe))
                {
                    appName = appName.Remove(appName.LastIndexOf(STR_Vshostexe), STR_Vshostexe.Length) + ".exe";
                }
    
                return appName + ".config";
            }
        }
    }
    

    I also added “TimeStamp” attribute to MySettings in app.config to check the result easily.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="MySettings" type="TestApp.MySettings, TestApp"/>
      </configSections>
    
      <MySettings CustomerName="" EmailAddress="" TimeStamp=""/>
    </configuration> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a problem with .NET generics. The thing I want to do
I am using C# language. I am facing one problem, please see the details
I have a bean structure to parse JSon files. I am facing one problem,
i am facing one font problem, my website is to operate in five languages,
I'm facing a hard problem, I'm developing an app to capture video from both
I am facing a problem. I have one query Select * from tabA where
I am writing a very basic web spider in java.I am facing one problem,
Currently I am working with Oracle identity federation 10.1.4.0.1. I am facing one problem
Im facing a problem where i want to schedule a certain java application to
I am facing problem with an Oracle Query in a .net 2.0 based windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.