Where do settings get saved once the application is running?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrei Pistol
    New Member
    • Sep 2011
    • 26

    Where do settings get saved once the application is running?

    Hello
    I would like to find out where (in what file) do settings get saved when my application runs (made with windows forms - after i publish my application, not when i debug it). All my settings are saved with the Scope being "User" and the default value being 0.
    For example i got this code
    Code:
    int x;
    x = Properties.Settings.Default.xSetting;
    private void buttonAdd_Click(object sender, EventArgs e)
    {
      x++;
    }
    private void buttonSave_Click(object sender, EventArgs e)
    {
      Properties.Settings.Default.xSetting = x;
    }
    So when i click the buttonAdd button it increase the x, when i click the buttonSave it saves the setting. I know where the default setting is saved, i cant find any file where the new settings are saved, the ones that change from one runtime to the other.

    Thanks anticipated.
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    You can simply write it into a text file.
    For more advance options, save it into embed database, such as SQLite.

    Comment

    • Andrei Pistol
      New Member
      • Sep 2011
      • 26

      #3
      Yes that did cross my mind (about the txt file) but i was hoping i can find them somewhere in an xml file (since the application knows the saved settings after closing and reopening it)

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        He's talking about the built in settings class.

        Here's the MSDN page for Settings. Take a look at the section entitled, "Changing the Value of a Setting Between Application Sessions"



        There should be a file somewhere (probably your executable folder) with a name that looks like: <AssemblyName>. exe.config

        Comment

        • Andrei Pistol
          New Member
          • Sep 2011
          • 26

          #5
          I've looked over that several times, I got a [appname].exe.config.dep loy (after i install it on my computer) and one [appname].exe.deploy
          The problem is that the .exe.deploy one is unreadable and the first one contains all the settings with the 0 value, even though in my application they increase and save themselves reloading the same values when i reopen the application.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            Oh are you running this as a ClickOnce application?

            Comment

            • Andrei Pistol
              New Member
              • Sep 2011
              • 26

              #7
              Yes, indeed i am :)
              Ive heard people using others but its kinda complicated for me to find another program to compile it, i would though if it would show me the settings file!

              Comment

              • GaryTexmo
                Recognized Expert Top Contributor
                • Jul 2009
                • 1501

                #8
                Ahhhh, you should have mentioned that at the start :D

                The thing with ClickOnce is, the application folder gets set to something really strange and can be pretty tough to find. I think it's in something like (Vista/Win7):

                c:\users\<you>\ appdata\local\a pps\<random set of chars>\<random set of chars>\<somethi ng that looks logically reasonable>

                That probably looks confusing, but try to follow it and hopefully you'll see what I mean. If you're using XP or another OS, just google "ClickOnce installation location <your OS name here>" and you'll likely find it. Anyway, your config file should be in there.

                Another option might be to put some code in your program to output the startup folder or the executable path. Take a look at the Application Class as there's all sorts of helpful stuff in there :) So for example, in your Form's Load event, you could put something like...

                Code:
                MessageBox.Show(Application.ExecutablePath);
                or you might even want to output it to a textbox somewhere so you can copy/paste it (I recommend some kind of debug window... basic example here: http://bytes.com/topic/c-sharp/insig...y-debug-window).

                Are you just looking for this to validate some of the settings, or do you actually need to edit it by hand sometimes? If the latter, you might want to look at storing it in the user's folder instead of the application folder. I believe you can manually move this around but you'll have to do some digging to find out how, as I don't know off the top of my head.

                Comment

                • Andrei Pistol
                  New Member
                  • Sep 2011
                  • 26

                  #9
                  Thanks mate, i couldn't find it though, the folders were either empty or full of clicksomething . manifest or so, no config whatsoever. I decided i should stop using settings to save my stuff, and instead save them to an .xml file with xmlreader since as ive heard, it more efficient (resource-wise)
                  But thank you very much

                  Comment

                  • GaryTexmo
                    Recognized Expert Top Contributor
                    • Jul 2009
                    • 1501

                    #10
                    There should be a folder in there called <blahblah>_ap p maybe? I honestly can't remember and don't have a ClickOnce application handy to test with.

                    Also, while I too would advocate handling it yourself over using Microsoft's built-in way, you're going to run into the same problem unless you store it somewhere specific (not recommended). Anything running out of the executable folder will be in that goofy ClickOnce installation path.

                    Comment

                    • Andrei Pistol
                      New Member
                      • Sep 2011
                      • 26

                      #11
                      I've found the path with
                      Code:
                      MessageBox.Show(Application.ExecutablePath);
                      Great thinking btw :D , sadly the config file there is with the default values. Will make the XML file and check if it turns up in there. Thank you

                      Comment

                      • arie
                        New Member
                        • Sep 2011
                        • 64

                        #12
                        To get the path of your user config file (when the application is installed and running), first add the reference to System.Configur ation.dll. Next execute the code:
                        Code:
                        string path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
                        In the localization Application.Exe cutablePath there are only Application settings, and in /bin folder of your project there is a file where you'll see your User settings, but they won't change. The real path is the one you'll get using the code above.

                        Comment

                        • Andrei Pistol
                          New Member
                          • Sep 2011
                          • 26

                          #13
                          I'm sorry but it pops 3 errors:
                          1. it wont allow me to add ".dll" at the system.configur ation
                          2. it says that ConfigurationMa nager does not exist in the current context
                          3. it says that ConfigurationUs erLevel does not exist in the current context

                          I've removed the .dll and that fixed one problem.

                          Comment

                          • arie
                            New Member
                            • Sep 2011
                            • 64

                            #14
                            In your Solution Explorer click References and add System.Configur ation (it'll be called this, without .dll extension, although on disc, in your Framework folder, it has it :)). Then add
                            Code:
                            using System.Configuration;
                            to your code. Then it'll work.

                            Comment

                            • Andrei Pistol
                              New Member
                              • Sep 2011
                              • 26

                              #15
                              Awesome, it worked. Thanks a lot :D

                              Comment

                              Working...