2

I'm trying to use properties.settings for the first time in a C# WPF project.

I have this code:

Settings.Default.comPort = "";
Settings.Default.baudRate = 9600;
Settings.Default.startUp = false;
Settings.Default.taskBar = true;
Settings.Default.desktopNotif = false;
Settings.Default.reset = false;
Settings.Default.Save();  

But it didn't work and the settings wasn't saved. So I added this line

Console.WriteLine("test");

after that part, to check if it goes through the code, and it executed fine.
So It seems that it just jumps over those lines without execution or even throwing any exceptions.

Amir
  • 1,885
  • 3
  • 19
  • 36
  • 2
    How do you know they are not saved? [Which file are you looking at](http://stackoverflow.com/a/33237394/69809)? Does [this thread](http://stackoverflow.com/q/469742/69809) answer your question? – vgru Mar 21 '16 at 10:07
  • Where did you check for the changes? `Default` means *default*, they are the *initial* values stored in your config file, *not* those saved in the user's config file. Or you tried to change application-scoped properties which aren't saved – Panagiotis Kanavos Mar 21 '16 at 10:07
  • I used two ways to test if they are saved. first, I commented out these lines and tried to read the value, but it was null. and second, I checked the app.config file and it contains the fields but the values were null – Amir Mar 21 '16 at 10:11
  • @Amir that didn't test anything. If you are trying to change application values, nothing will be saved anyway. User-scoped values are stored in the user's AppData\AppName folder – Panagiotis Kanavos Mar 21 '16 at 10:11
  • 2
    Did you check the AppData folder then ? Don't assume there is a bug in such basic functionality. People would have noticed long ago. You need to isolate what is actually happening. For example, if you are debugging the application, the settings may get cleared each time a change is detected. If you add/remove/change a setting, you need to call `Upgrade` to read the *old* settings. Otherwise your code would crash trying to read older, incompatible values – Panagiotis Kanavos Mar 21 '16 at 10:14
  • @Panagiotis Kanavos So you mean it might be because I run my code in the debug mode? – Amir Mar 21 '16 at 10:18
  • @Amir not exactly - different builds may result in different assembly numbers if you've used `*` as the minor version. Different assembly numbers use a different `appdata` folder to avoid mixing data from different versions. You'd have to check and upgrade settings anyway, as shown [in this question](http://stackoverflow.com/questions/534261/how-do-you-keep-user-config-settings-across-different-assembly-versions-in-net/534335#534335): if `UpgradeRequired` is true, call `Upgrade`, set the flag to `false` and save – Panagiotis Kanavos Mar 21 '16 at 10:29

1 Answers1

3

Thanks to Panagiotis Kanavos I eventually found the problem. The code was correct, but when the program runs inside the visual studio, every time the settings are being overwritten. After building, I ran the .exe file in the bin/release folder and it worked just fine.

Community
  • 1
  • 1
Amir
  • 1,885
  • 3
  • 19
  • 36