Share via


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

Question

Wednesday, October 18, 2006 2:42 AM | 1 vote

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?

 

 

All replies (36)

Thursday, October 19, 2006 5:35 PM ✅Answered | 3 votes

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


Friday, November 3, 2006 6:11 PM ✅Answered | 3 votes

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


Sunday, October 22, 2006 4:36 PM | 2 votes

 

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 6:22 PM | 1 vote

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")]


Monday, October 23, 2006 2:03 AM

Thank You for the information.

I learn something new everyday!


Friday, November 3, 2006 6:05 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 3, 2006 11:18 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 3, 2006 2:42 PM

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 3, 2006 3:56 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 3, 2006 9:40 PM

That is it! That works perfectly! Thanks a lot for your help!


Friday, November 3, 2006 10:26 PM

you welcome


Saturday, November 4, 2006 3:50 AM

Thank You for the code. I'll give it a try.

Have a GREAT day..


Monday, June 11, 2007 6:28 PM

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 9:23 PM

Did you ever get this figured out?  I am having the exact same problem and none of the solutions provided have helped.

Thanks


Saturday, July 12, 2008 8:00 AM

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.


Monday, February 2, 2009 5:57 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.


Friday, February 13, 2009 8:49 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.)


Thursday, March 26, 2009 8:28 PM

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


Tuesday, May 12, 2009 3:06 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.


Wednesday, January 13, 2010 1:57 AM

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 6:58 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 :)


Saturday, February 6, 2010 7:20 PM | 1 vote

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


Thursday, July 8, 2010 7:08 AM | 1 vote

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


Friday, November 12, 2010 9:01 PM | 1 vote

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.


Wednesday, February 9, 2011 12:12 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


Friday, February 11, 2011 4:40 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

 

 


Monday, April 18, 2011 6:09 AM | 1 vote

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


Thursday, May 12, 2011 8:39 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().

.NET编程常见问题: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中文技术论坛


Friday, February 1, 2013 10:41 PM

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


Thursday, March 7, 2013 5:01 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.


Saturday, April 13, 2013 1:10 AM

I had the same thing.  For me it was as simple as changing my CompanyName from "" to "XYZ Inc" in AssemblyInfo.cs 


Thursday, January 23, 2014 9:57 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à!


Friday, July 24, 2015 6:25 PM

I had this same problem and tore my hair out for 3 days.

I found the USER scope settings get put into a HIDDEN directory under

c:\users\user>\AppData\programname>\programname>.xxxxsomejunkherexxx\1.7.0\user.config

Where the 1.7.0 is the setting from your AssemblyInfo.cs file

[assembly: AssemblyVersion("1.7.0")]

but if you put a * at the end, it created a new one every build and so you are always looking in a new sub directory for user.config.  (I am not sure if Upgrade() fixes this- but read on...)

Then I discovered something VERY ODD!!

I was doing Properties.Settings.Default.Save(); inside the SelectionChanged event handler for a combo box.  THIS BLEW AWAY THE SETTINGS EVERY TIME

Removing it from that event handler fixed the problem

I hope I have saved someone else some headaches.  

Remove ALL  Properties.Settings.Default.Save(); from your code EXCEPT for one known location and see if that fixes your problem of not seeing your changes saved.  

Then add them back in and keep checking - something is broken on that.


Thursday, December 22, 2016 2:07 PM

It should be widely noted that having an asterisk in your AssemblyInfo.cs source file (Properties Folder of your Project, see Solution Explorer) for the Auto AssemblyVersion incrementation; will cause the user.config to be started fresh each Run/Debug/Build and you won't see these values coming back on subsequent runs because they are reverting to the defaults setup in Settings.settings each build/run. Changing this to a hard coded int value will allow the user.config to remain across multiple runs and you will, then and only then, see the saved Settings coming back to you on subsequent Debug & Test runtimes.

It should further be noted that the values you are seeing and possibly watching in Setting.settings Designer will not be changed programmatically as these are the initial default values. These values will be used in Settings.Default.Reset(); calls which effectively delete user.config and starts it fresh. So WATCH for value changes of Settings coming back in in the WATCH list of your Debugger rather than expecting the Setup Table Values to ever change programmatically.

Sr Software Developer Global Access Computers and Entertainment Systems


Thursday, December 22, 2016 2:19 PM

See mine above, I think it will help many people. I spent hours on this a while back and it was a hard-to-phrase "question" and difficult-to-find answer, but so darn simple I'd hate to see a good developer feeling stupid over something like an asterisk in your Assembly.Info.cs for the AssemblyVersion to auto increment builds. Causing the Settings saved in the user.config to be Reset to Default / initial values each Debug/Run/Build. Your votes will tell me how many this helps. Happy coding!

Sr Software Developer Global Access Computers and Entertainment Systems


Friday, March 15, 2019 6:35 AM

After trying, I find that when you save,  the value is not saved to  config file, but seems to save to 

%appdata%\\Roaming\\[AssemblyCompany]\\[AssemblyName]\\[AssemblyVersion]

 by  

this reddit article

so though content of config file doen't change, it's truely saved.