How to store values in app.config or settings.cs

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Sm9u?=

    How to store values in app.config or settings.cs

    Hello,
    I have tried to use the app.config and settings.cs files to store my data
    (which I want to be user changeable at runtime). I can write to (what I
    assume is an object in memory) and it does seem to work...however, once the
    application is closed and reopened the changes are lost. How do I persist
    the information?


    Here is some sample code using both methods:
    //////Using Properties.Sett ings///////
    //get the key from Properties.Sett ings
    string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
    //This line seems to only set the value in memory and not in the file.
    Properties.Sett ings.Default["myKey"] = "myNewValue ";
    //myPropertiesSet ting will now be equal to "myNewValue "
    myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
    //things look good, except when I restart the application the new value
    //was not stored in the settings.cs file.


    //////Using ConfigurationMa nager.AppSettin gs///////
    NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
    //get the key from ConfigurationMa nager.AppSettin gs
    String myAppSetting = appSettings["myKey"];
    //This line seems to only set the value in memory and not in the file.
    appSettings["myKey"] = "myNewValue ";
    //appSettings will now be equal to "myNewValue "
    myAppSetting = appSettings["myKey"];
    //things look good, except when I restart the application the new value
    //was not stored in the app.config file.
    Thanks,
    Jon


  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: How to store values in app.config or settings.cs

    I know this might not be the recommended way of doing it, but I normally
    create a a project for configuration objects which I mark as Serializable. I
    then serialize the objects out of and into xml in a file in the same folder
    as my app.
    I make a class like AppConfiguratio n which has app settings. Then I make
    other classes for specific settings for specific parts of the app and add a
    property for it to AppConfiguratio n. I make AppConfiguratio n a singleton and
    have it load and save itself automatically with the XmlSerializer, you can
    even precompile the XmlSerializatio n assemblies for it.
    This is a basic description, let me know if you like the general idea and
    I'll slap a demo of it up on my blog.


    --
    Ciaran O''Donnell
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "Jon" wrote:
    Hello,
    I have tried to use the app.config and settings.cs files to store my data
    (which I want to be user changeable at runtime). I can write to (what I
    assume is an object in memory) and it does seem to work...however, once the
    application is closed and reopened the changes are lost. How do I persist
    the information?
    >
    >
    Here is some sample code using both methods:
    //////Using Properties.Sett ings///////
    //get the key from Properties.Sett ings
    string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
    //This line seems to only set the value in memory and not in the file.
    Properties.Sett ings.Default["myKey"] = "myNewValue ";
    //myPropertiesSet ting will now be equal to "myNewValue "
    myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
    //things look good, except when I restart the application the new value
    //was not stored in the settings.cs file.
    >
    >
    //////Using ConfigurationMa nager.AppSettin gs///////
    NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
    //get the key from ConfigurationMa nager.AppSettin gs
    String myAppSetting = appSettings["myKey"];
    //This line seems to only set the value in memory and not in the file.
    appSettings["myKey"] = "myNewValue ";
    //appSettings will now be equal to "myNewValue "
    myAppSetting = appSettings["myKey"];
    //things look good, except when I restart the application the new value
    //was not stored in the app.config file.
    Thanks,
    Jon
    >
    >

    Comment

    • G.S.

      #3
      Re: How to store values in app.config or settings.cs

      On Sep 29, 10:13 pm, Jon <J...@discussio ns.microsoft.co mwrote:
      Hello,
      I have tried to use the app.config and settings.cs files to store my data
      (which I want to be user changeable at runtime).  I can write to (what I
      assume is an object in memory) and it does seem to work...however, once the
      application is closed and reopened the changes are lost.  How do I persist
      the information?
      >
      Here is some sample code using both methods:
      //////Using Properties.Sett ings///////
      //get the key from Properties.Sett ings
      string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
      //This line seems to only set the value in memory and not in the file.
      Properties.Sett ings.Default["myKey"] = "myNewValue ";
      //myPropertiesSet ting will now be equal to "myNewValue "
      myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
      //things look good, except when I restart the application the new value
      //was not stored in the settings.cs file.
      >
      //////Using ConfigurationMa nager.AppSettin gs///////
      NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
      //get the key from ConfigurationMa nager.AppSettin gs
      String myAppSetting = appSettings["myKey"];
      //This line seems to only set the value in memory and not in the file.
      appSettings["myKey"] = "myNewValue ";
      //appSettings will now be equal to "myNewValue "
      myAppSetting = appSettings["myKey"];
      //things look good, except when I restart the application the new value
      //was not stored in the app.config file.
      Thanks,
      Jon
      I have found this article (and its links) to provide helpful
      information in that direction


      Comment

      • =?Utf-8?B?Sm9u?=

        #4
        RE: How to store values in app.config or settings.cs



        "Ciaran O''Donnell" wrote:
        I know this might not be the recommended way of doing it, but I normally
        create a a project for configuration objects which I mark as Serializable. I
        then serialize the objects out of and into xml in a file in the same folder
        as my app.
        I make a class like AppConfiguratio n which has app settings. Then I make
        other classes for specific settings for specific parts of the app and add a
        property for it to AppConfiguratio n. I make AppConfiguratio n a singleton and
        have it load and save itself automatically with the XmlSerializer, you can
        even precompile the XmlSerializatio n assemblies for it.
        This is a basic description, let me know if you like the general idea and
        I'll slap a demo of it up on my blog.
        >
        >
        --
        Ciaran O''Donnell
        try{ Life(); } catch (TooDifficultException) { throw Toys(); }

        >
        >
        "Jon" wrote:
        >
        Hello,
        I have tried to use the app.config and settings.cs files to store my data
        (which I want to be user changeable at runtime). I can write to (what I
        assume is an object in memory) and it does seem to work...however, once the
        application is closed and reopened the changes are lost. How do I persist
        the information?


        Here is some sample code using both methods:
        //////Using Properties.Sett ings///////
        //get the key from Properties.Sett ings
        string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
        //This line seems to only set the value in memory and not in the file.
        Properties.Sett ings.Default["myKey"] = "myNewValue ";
        //myPropertiesSet ting will now be equal to "myNewValue "
        myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
        //things look good, except when I restart the application the new value
        //was not stored in the settings.cs file.


        //////Using ConfigurationMa nager.AppSettin gs///////
        NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
        //get the key from ConfigurationMa nager.AppSettin gs
        String myAppSetting = appSettings["myKey"];
        //This line seems to only set the value in memory and not in the file.
        appSettings["myKey"] = "myNewValue ";
        //appSettings will now be equal to "myNewValue "
        myAppSetting = appSettings["myKey"];
        //things look good, except when I restart the application the new value
        //was not stored in the app.config file.
        Thanks,
        Jon
        Thanks Ciaran,
        I would love an example of what you are talking about if you have time
        to provide it. But, I really was wanting to use the built in C# framework
        for this because it can do a ton of work I unless it doesn't do what I need,
        I don't want to have to roll my own code. (although I would love to know how
        just in case I need to : )
        BTW, I think I did figure out how to use the ConfigurationMa nager class
        to save to the file although I wish there was a replace() instead of just a
        remove() and an add()..here is the code I came up with, let me know if you
        have any better suggestions : ) Thanks again.
        Jon

        Configuration myConfigObject =
        ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None);

        //try to read the value...should be null the first time and then have a
        value after that
        KeyValueConfigu rationElement rootKVE =
        myConfigObject. AppSettings.Set tings["rootDrive"];

        myConfigObject. AppSettings.Set tings.Remove("r ootDrive");

        myConfigObject. AppSettings.Set tings.Add(new
        KeyValueConfigu rationElement(" rootDrive", "J:\\"));

        myConfigObject. Save();

        Comment

        Working...