none
Properties.Settings.Default.Save(); does not save a user.config file.

    Question

  • I have settings that are in the user scope and I can update them at runtime. When I call the save method,nothing gets saved. Any ideas?

     

     

    Wednesday, October 18, 2006 2:42 AM

Answers

  • This is working. Had to perform a Properties.Settings.Default.Upgrade() and then my saved settings get loaded. 
    Thursday, October 19, 2006 5:35 PM
  • hi,

    will george its not that hard, here its how can you do it

    1. Add the proper setting to you Settings.settings file

    in your solution explorer expand properties node and double click Settings.settings file, use the UI to add your properties in my case i added 2

    Name                        type                                  scope                  Defaultvalue

    Form1Location       System.Drawing.Point       user                       10,10

    Form1Size              System.Drawing.Size        user                       250,250

    then save the settings this will make 2 more nodes appear in your app.config

    <configSections>

    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

    <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />

    </sectionGroup>

    </configSections>

    <userSettings>

    <WindowsApplication1.Properties.Settings>

    <setting name="Form1Location" serializeAs="String">

    <value>10, 10</value>

    </setting>

    <setting name="Form1Size" serializeAs="String">

    <value>250, 250</value>

    </setting>

    </WindowsApplication1.Properties.Settings>

    </userSettings>

    ok now how to use this in code


    public partial class Form1 : Form

    {
    public Form1()
    {
    InitializeComponent();
    //Load saved settings

    this.Location = Properties.Settings.Default.Form1Location;
    this.Size = Properties.Settings.Default.Form1Size;
    //Allow changes to be implemented

    this.StartPosition = FormStartPosition.Manual;
    //capture changes

    this.LocationChanged += new EventHandler(Form1_LocationChanged);
    this.SizeChanged += new EventHandler(Form1_SizeChanged);
    //capture the closing form event to save your new settings

    this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
    }
    void Form1_LocationChanged(object sender, EventArgs e)
    {
    //Capture the new values

    Properties.Settings.Default.Form1Location = this.Location;
    }
    void Form1_SizeChanged(object sender, EventArgs e)
    {
    //Capture the new values

    Properties.Settings.Default.Form1Size = this.Size;
    }
    void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
    //you can capture the new values here as well

    //Properties.Settings.Default.Form1Location = this.Location;

    //Properties.Settings.Default.Form1Size = this.Size;

    //save the new values

    Properties.Settings.Default.Save();
    }
    }


     

    this is a nice article on msdn about how to use settings http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/SettingsCS_RL.asp

    plz note the app.config in your solution explorer and app.exe.config in yourProjectFolder\bin\debug will hold the default values , so don't expect you will find the chanes in those two files, but when the values changed in your app it will be saved where Rod Yager has said,

    hope this helps

    Friday, November 03, 2006 6:11 PM

All replies

  • This is working. Had to perform a Properties.Settings.Default.Upgrade() and then my saved settings get loaded. 
    Thursday, October 19, 2006 5:35 PM
  •  

    This is working. Had to perform a Properties.Settings.Default.Upgrade() and then my saved settings get loaded.

    I saw .. and then my saved settings get loaded. 

    I almost got loaded trying to solve this one! I still do not have it saved!

    I'm not the sharpest Tack in the factory but if you hammer on me long enough (and I do not bend over)  I can hold the fabric....

    Anyway, seems so simple till' ya' try to do it! No matter what I try I still end up with the original settings when I restart the program. I guess my real question is in what order and when do you do your 'Properties.Settings.Default.Upgrade() ' and when do you do the save? I'm so lost..  LOL... So bad here it is really funny!

    I tried things like

    Properties.Settings.Default.DCS900W_IP = textBox_WebCamAddress.Text;
    Properties.Settings.Default.Save();
    Properties.Settings.Default.Upgrade();
    Properties.
    Settings.Default.Save();
    (Many different orders)  AND do a Properties.Settings.Default.Upgrade();  On initialize of the program.

    All in all I end up with what I put into the 'Setting' in the first place.. Geeeeeeeeeee.. HELP!

    Sunday, October 22, 2006 4:36 PM
  • First verify that the user.config file is acutally being created. It should be under C:\Documents and Settings\username\Local Settings\Application Data\CompanyName. It will only be visible when you allow "show hidden files and folders" under your folder options. The Company Name should be set in your AssemblyInfo.cs under

    [assembly: AssemblyCompany("CompanyName")]

    Sunday, October 22, 2006 6:22 PM
  • Thank You for the information.

    I learn something new everyday!

    Monday, October 23, 2006 2:03 AM
  • In my case it is saving the settings in the App.Config file but I can't get it to load or save any settings. Here is what I have:

     <userSettings>
      <namespace
    .Properties.Settings>
        <
    setting name="WindowLocation" serializeAs="String">
          <
    value>0, 0</value>
        </
    setting>
        <
    setting name="WindowSize" serializeAs="String">
          <
    value>363, 359</value>
        </setting>
      </
    namespace.Properties.Settings>
    </
    userSettings>

    Here is the code to load the preferences:

    if (Settings.Default.WindowLocation != null)
    {
      this.Location = Settings.Default.WindowLocation;
    }
    if (Settings.Default.WindowSize != null)
    {
      this.Size = Settings.Default.WindowSize;
    }

    Here is the code to save the preferences:

    Settings.Default.WindowLocation = this.Location;
    if (this.WindowState == FormWindowState.Normal)
    {
      Settings.Default.WindowSize = this.Size;
    }
    else
    {
      Settings.Default.WindowSize = this.RestoreBounds.Size;
    }
    Settings.Default.Save();

     

    I have tried everything I can think of and I can't get this to work. Any help would be greatly appreciated.

    Friday, November 03, 2006 6:05 AM
  • If your App.Config file is stored with the program under Program Files somewhere, a normal user (non-admin, non-power user) will NOT be able to write to that file. Something to be aware of...
    Friday, November 03, 2006 11:18 AM
  • Yes, the App.Config is stored in the program files but I am running as Admin so it shouldn't be an issue. I wouldn't normally do this but I am creating a small app as code sample for a job and one of the requirements is to store and update the window size and location in the App.Config file. Any other ideas?
    Friday, November 03, 2006 2:42 PM
  • I gave up on doing that.. I'm not that smart.. I just made info in the registry and works OK!

    Wondering why THAT should be so HARD!

    Friday, November 03, 2006 3:56 PM
  • hi,

    will george its not that hard, here its how can you do it

    1. Add the proper setting to you Settings.settings file

    in your solution explorer expand properties node and double click Settings.settings file, use the UI to add your properties in my case i added 2

    Name                        type                                  scope                  Defaultvalue

    Form1Location       System.Drawing.Point       user                       10,10

    Form1Size              System.Drawing.Size        user                       250,250

    then save the settings this will make 2 more nodes appear in your app.config

    <configSections>

    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

    <section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />

    </sectionGroup>

    </configSections>

    <userSettings>

    <WindowsApplication1.Properties.Settings>

    <setting name="Form1Location" serializeAs="String">

    <value>10, 10</value>

    </setting>

    <setting name="Form1Size" serializeAs="String">

    <value>250, 250</value>

    </setting>

    </WindowsApplication1.Properties.Settings>

    </userSettings>

    ok now how to use this in code


    public partial class Form1 : Form

    {
    public Form1()
    {
    InitializeComponent();
    //Load saved settings

    this.Location = Properties.Settings.Default.Form1Location;
    this.Size = Properties.Settings.Default.Form1Size;
    //Allow changes to be implemented

    this.StartPosition = FormStartPosition.Manual;
    //capture changes

    this.LocationChanged += new EventHandler(Form1_LocationChanged);
    this.SizeChanged += new EventHandler(Form1_SizeChanged);
    //capture the closing form event to save your new settings

    this.FormClosed += new FormClosedEventHandler(Form1_FormClosed);
    }
    void Form1_LocationChanged(object sender, EventArgs e)
    {
    //Capture the new values

    Properties.Settings.Default.Form1Location = this.Location;
    }
    void Form1_SizeChanged(object sender, EventArgs e)
    {
    //Capture the new values

    Properties.Settings.Default.Form1Size = this.Size;
    }
    void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
    //you can capture the new values here as well

    //Properties.Settings.Default.Form1Location = this.Location;

    //Properties.Settings.Default.Form1Size = this.Size;

    //save the new values

    Properties.Settings.Default.Save();
    }
    }


     

    this is a nice article on msdn about how to use settings http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/SettingsCS_RL.asp

    plz note the app.config in your solution explorer and app.exe.config in yourProjectFolder\bin\debug will hold the default values , so don't expect you will find the chanes in those two files, but when the values changed in your app it will be saved where Rod Yager has said,

    hope this helps

    Friday, November 03, 2006 6:11 PM
  • That is it! That works perfectly! Thanks a lot for your help!
    Friday, November 03, 2006 9:40 PM
  • you welcome
    Friday, November 03, 2006 10:26 PM
  • Thank You for the code. I'll give it a try.

    Have a GREAT day..

    Saturday, November 04, 2006 3:50 AM
  • Why won't this work?

                double dou = OneWireLearning.Properties.Settings.Default.ccwLargeA;
                OneWireLearning.Properties.Settings.Default.cwLargeA = 9.876543;
                double dou2 = OneWireLearning.Properties.Settings.Default.cwLargeA;
                OneWireLearning.Properties.Settings.Default.Save();

    The value of cwLargeA is 9.876543 during runtime, but after closing the program and checking the settings file, it's back to it default value.

    Anyone see the problem?

    Thanks
    Monday, June 11, 2007 6:28 PM
  • Did you ever get this figured out?  I am having the exact same problem and none of the solutions provided have helped.

    Thanks
    Monday, June 11, 2007 9:23 PM
  • The save works for me.  I call it when my window closes.  Also, even though the value has changed and is saved, the settings panel inside VS still shows the old values.  However, at runtime, the new value appears.
    Saturday, July 12, 2008 8:00 AM
  • If anyone has come to this post and still confused as to why the settings will save, it's actually very easy.  The only settings that you can save are USER settings.  By default when you create an entry in the Settings.Settings file it gets set up as an Application setting which can not be saved during run time.  Check this setting.
    • Proposed as answer by fpna Monday, February 02, 2009 5:57 AM
    Monday, February 02, 2009 5:57 AM
  •  Properties.Settings.Default.Save() still does not save on my system.  On a different system (in an organisation where I work as volunteer in order to gain experience, in the following I call it "System A") it works.  System A seems to generate automatically a user.config file to which it saves the settings and from where it loads them back with next startup.  Not so on my own system which I here shall call "System B."

    I have spent the last couple of hours searching in this and other user fora.  There are lots of threads about not saving. I am  pretty sure that I have not made any of the errors described in the responses.  But I did not find the problem mentioned below described.  I shall be grateful for any help.

    Here is what I do on both systems:

    (1) In Settings.settings creates (for example) a setting Left, int, user, 100.
    (2) In formload the following code: {this.Left = Properties.Settings.Default.Left;}
    (3) In formclosure the following code:
        {Properties.Settings.Default.Left = this.Left;
          Properties.Settings.Default.Save();}

    In System A, if I in a session has changed the location of the form in the next session the new Left property is used to position the form.  Not so in System B.

    I am an amateur so I have rambled around a bit.  I noticed in System A that if I in the Settings designer press "Synchronize" it will warn me that it is about to delete the file:

    C:\Documents and Settings\CJORGENS\Local Settings\Application Data\Rode_Kruis-Vlaanderen\ACTClientException4-vshos_Url_unbe2myuo0jnp00pqtj2hjk0r5gijmf\1.0.0.0\user.config

    When I have made changes I can search for and find that file.  It is an XML file.  Before I have made any changes or after I have synchronized that file does not exist.  When I press Synchronize it responds: 

    "No user.config files were found in any of the following locations" followed by a list of directories one of which is  "C:\Documents and Settings\CJORGENS\Local Settings\Application Data\Rode_Kruis-Vlaanderen\ACTClientException4-vshos_Url_unbe2myuo0jnp00pqtj2hjk0r5gijmf\1.0.0.0"

    In system B when I press Synchronize I get the message:  "No user.config files were found in any of the following locations:"  and nothing else.

    Thus, system A knows where to make the user.config files (I have searched wide and broad to learn how system A knows that but in vain) whereas system B seems not to have that knowledge, and this seems to be why it does not save and reload user settings.  How can resolve it?

    For system B I use Microsoft Visual Studio 9.0 sitting in C:\Program Files\.  The project (named "Trial") sits in C:\Documents and Settings\Christian\My Documents\Visual Studio 2008\Projects\Trial\Trial.  In That directory I have in bin\Debug an XML file Trial.vshost.exe.config and in that file I find, among others (100 is the default value for Left)

    <setting name="Left" serializeAs="String">
    <
    value>100</value>
    </
    setting>

    This became a long question.  However, I tried to be as specific as possible with the hope that someone can help me (if you did not fall asleap reading it.)

    Friday, February 13, 2009 8:49 AM
  • Dear Sir\Madam:


    I created an Options Button to allow the user to modify and save printer settings because my project has large amount of pages for printing. User can save printer settings for simpler printing.

    So i used "Application Settings". I created a setting in type of "System.Drawing.Printing.PrinterSettings" and with "User" scope.(name of it is "mySavedSettings")

    But Save() function does not work. In next run program, "mySavedSettings" is null!

    I tried other types such as Font, Color, Bool, Int without any problem.

    Please help me
    Thursday, March 26, 2009 8:28 PM
  • Wonder if you received an answer to your question. I'm having the same problem with some user's not being able to save settings yet others are fine.
    Tuesday, May 12, 2009 3:06 PM
  • I am having the same issue on my app and I have pulled most of my hair out.  This really shouldn't be so difficult.  I am using user settings rather than application settings so that isn't the problem.  I have no idea what is causing this but it is a big problem!  HELP!!!
    Wednesday, January 13, 2010 1:57 AM
  • OK...

    I think this is because some variable types has loop properties!
    For example, variable type A has this properties: int  A.var1, char  A.var2, B  A.var3
    variable type B has this properties: float  B.var21,  decimal  B.var22,  A  B.var23

    As you mention, type A has a property of B type and B has a property of A type. Therefore a loop has been occurred (A.B.A.B.A. ...)

    I suggest you to save any "clear" property in separately.

    For example, B.var21 and B.var22 and A.var1 and A.var2 as you need.

    Best Regards,
    Mousavi - An Iranian programmer :)
    Wednesday, January 13, 2010 6:58 AM
  • If the Settings scope is set to USER, the exe.config file is saved to the user directory: C:\Documents and Settings\[user]\Local Settings\Application 
    Data\[application_name] folder.

    If the scope is set to USER, the current exe.config file (in your program files/app directory) is just used to load default settings.

    -a
    • Proposed as answer by Adam Hubert Saturday, February 06, 2010 7:20 PM
    Saturday, February 06, 2010 7:20 PM
  • I found that I had to do a Reload() first:

                Properties.Settings.Default.Reload();

                string savedSetting = Properties.Settings.Default.SavedSetting; 

    It is saved as Adam Hubert says in C:\Documents and Settings\[user]\Local Settings\Application 

    Data\[application_name] folder


    Thursday, July 08, 2010 7:08 AM
  • I had this same problem and realized what was happening. I had the assembly version set so 1.0.* which causes the revision to update with every build. The user settings saved in C:\Documents and Settings\username\Local Settings\Application Data\CompanyName\AppName\Version are stored by revision, so every time I built, the settings previously saved under the previous revision did not show up for the new revision. After changing the the assembly version to 1.0.0.0, this stopped happening and the changes showed up again.
    Friday, November 12, 2010 9:01 PM
  • I concur with Will,

    I see all sorts of "It's easy....just do this and that", and it doesn't quite work.  Saving and loading multiple-character strings works just fine.  Try saving a character - or go one step further - try saving a character from a ComboBox dropdown in a Forms app! I have been beating my head against the wall for two days on this one.  My most recent issue is that although I am performing a 'Save', the file never gets updated.

    Like Will, I'm not Einstein, but what I know I know well.  I will eventually get this working and let everyone know what I found.  Sometimes something simple that should take a few minutes ends up taking a few days.  That is the grand illusion that Marketing folks and bean counters just don't understand.


    MarkP
    Wednesday, February 09, 2011 12:12 PM
  • Hi All,

    This is how I got it worked. It's actually a mix of all of your suggestions.

    1. When you have to write to config file , it has to be at the  User Level, because in the application level, its readonly. so SET Scope : User

    2. This is the code I used to write to the config file,

    CFCRemoteScan.Properties.

    Settings.Default.ScanMode = "IsaacTest";

    3. After you write it, you should save this:

    Properties.

    Settings.Default.Save();

    4. When I did the save, I went back and saw if the app.config file has changed  but it didn't. So since we have set the SCOPE in USER,

    I went to the :

    C:\Documents and Settings\[Logged in Username]Local Settings\Application Data\[Application Folder/What ever company name the product is registered to]\[Project name].vshost.exe_StrongName_01dg2vgvaccigjkgn2phfw20g15qits1\[Assembly Version]\user.config

    *[Project name].vshost.exe_StrongName_01dg2vgvaccigjkgn2phfw20g15qits1 can be variable(VS Creates this for you, sort by Date Modified )

     

    Here I can see the user.config with the saved setting.

     

    <?

     

    xml version="1.0" encoding="utf-8"?>

    <

     

    configuration>

    <

     

    configSections>

    <

     

    sectionGroup name="userSettings">

    <

     

    section name="CFCRemoteScan.Properties.SpecialSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />

    </

     

    sectionGroup>

    </

     

    configSections>

    <

     

    userSettings>

    <

     

    TEST.Properties.Settings>

    <

     

    setting name="ScanMode" serializeAs="String">

    <

     

    value>IsaacTest</value>

    </

     

    setting>

    </

     

    TEST.Properties.Settings>

    </

     

    userSettings>

    </

     

    configuration>

    4. To load back your recent changes you have applied to this file,

    You must use this piece of code: before reading it,

    TEST.Properties.Settings.Default.Upgrade();

     

    string ScanMode = TEST.Properties.Settings.Default.ScanMode.ToString();

    ************************************************************

    Here is the order :

     

    private void frmMain_Load(object sender, System.EventArgs e)

    {

    CFCRemoteScan.Properties.

    Settings.Default.Upgrade();

    string ScanMode = CFCRemoteScan.Properties.Settings.Default.ScanMode.ToString();

    CFCRemoteScan.Properties.

    Settings.Default.ScanMode = "IsaacTest";

    }

     

     

    private void frmMain_FormClosed(object sender, FormClosedEventArgs e)

    {

     

    Properties.

    Settings.Default.Save();

     

    }

    Thank you.

    Isaac

     

     

    Friday, February 11, 2011 4:40 PM
  • In Visual Studio, go to Main Menu > Debug > Exceptions...,  and set it to break on all Common Language Runtime Exceptions.

    Sorry for the necro-bump, but none of the posted solutions worked for me. The file user.config wasn't even being created. In .Net 4.0 (haven't tested other versions), it seems that Settings.Save() suppresses any exceptions that occur. It always throw a FileNotFoundException on the first call, trying to load [name of the assembly containing your class].XMLSerializers. This is normal - it's just the .Net runtime figuring out that it hasn't built the serializers yet. After these, I got exceptions (via InnerException) saying that some of my types didn't have SerializableAttribute. Fixing the indicated errors solved the problem.


    Coder0xff
    Monday, April 18, 2011 6:09 AM
  • I have settings that are in the user scope and I can update them at runtime. When I call the save method,nothing gets saved. Any ideas?

     

     


    the correct file path should be : \Documents and Settings\frank.xu\Local Settings\Application Data\54PeiXun\TestApp.vshost.e_Url_q4ojm0fdp1byzaksoxfy2hvhupnzuabx\1.0.0.0\user.config.

    I have tested it,the file was updated after we call Properties.Settings.Default.Save().


    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
     

    老徐的网站】:http://www.frankxulei.com/

    微软WCF中文技术论坛
    微软WCF英文技术论坛

    Windows Azure中文技术论坛

    Thursday, May 12, 2011 8:39 AM
  • THANK YOU!!!!!

    I set debugging to all Common Language Runtime Exceptions as you suggested and discovered that my Outlook add-in's inability to correctly save/load the settings was due to my add-in project targeting the wrong framework.  I'm using Visual Studio 2010 and originally my project targeted the .NET 4.0 framework but I had to switch to 3.5 due to some incompatibility issues.  Anyway, once I changed the target framework to 3.5 for my add-in project, it seems Visual Studio was NOT smart enough to update my app.config file which still had a reference to 4.0.  By manually editing app.config to change all references from "Version=4.0.0.0" to "Version=2.0.0.0" (apparently .NET 3.5 still uses 2.0.0.0 in this case) in the <sectionGroup> element and its child elements, I was able to get the settings working again.

    Kudos to the following post which also helped point me in the right direction:

    http://stackoverflow.com/questions/6408308/application-settings-error-after-changing-target-framework-of-project

    Friday, February 01, 2013 10:41 PM
  • On Windows 7, you can find the users settings file under:

    C:\Users\<your domain name>\AppData\Local\<Your Assembly Name>.

    Under that folder you will find folders that look like this:

    <Your Assembly Name>.vshost.exe_Url_3akdsonpjvesa5qqcuxcdkjnazmrbv0p

    where the last part is related to the strong name of the assembly and the .vshost. part of the name is present if you ran the app from inside Visual Studio.

    Under that folder, you will find a folder for each assembly version as defined in the Properties\AssemblyInfo.cs file.

    Under that folder you will find a user.config file.

    Thursday, March 07, 2013 5:01 PM
  • I had the same thing.  For me it was as simple as changing my CompanyName from "" to "XYZ Inc" in AssemblyInfo.cs 
    Saturday, April 13, 2013 1:10 AM
  • I just spent some time on "WHA?" where my settings My.Settings.Save worked during runtime, but then when I shutdown and restarted they were lost.

    I went to where the user.config was located and deleted the various folders for that application (one for each version change) including the most recent.

    Then I ran the application, set and saved some My.Settings values, reviewed the user.config before and after close, and Voilà!

    Thursday, January 23, 2014 9:57 AM