0

I have made an exefile with winforms and I want to read/write settings to the exefiles configuration. But how can I do it? I'm using Visual Studio 2013. Is there differences between Visual Studio 2013 and 20196?

I have tried the following in c# but I couldn't find Configuration, ConfigurationManager and ConfigurationSaveMode - so what do I need to using?

Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Remove("CountryCode");
config.AppSettings.Settings.Add("CountryCode", "+45");
config.AppSettings.Settings.Remove("MobilePhone");
config.AppSettings.Settings.Add("MobilePhone", "12345678");
config.Save(ConfigurationSaveMode.Modified);
Michael Eriksen
  • 167
  • 1
  • 3
  • 13
  • _"I couldn't find"_ - please read [ask] and mention the exact compiler errors, as well as what you have tried to resolve those. – CodeCaster Aug 08 '19 at 14:35
  • It's not about the differences in Visual Studio, but rather differences in versions of the .NET Framework. But this hasn't really changed. You might want to look at [this](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings) instead. – Scott Hannen Aug 08 '19 at 14:38

1 Answers1

0

This isn't how settings works in winforms. Call up the properties for your project (right click the project, choose properties). Click settings. Enter your settings in there - for example make one called TimerInterval, of type int, user scope, value set to 3000

To use them from code do:

_myTimer.Interval = Properties.Settings.Default.TimerInterval;

To change settings, set like any other property:

Properties.Settings.Default.TimerInterval = 2000;

To save them do:

Properties.Settings.Default.Save();

Only user scoped settings can be altered/saved at runtime. Application scopes settings are altered by changing the Settings grid as above. It is intended you use user scopes settings for things the user will change /prefs and app scoped for things your app needs to run but you as developer want to configure (db connection string perhaps)

Caius Jard
  • 72,509
  • 5
  • 49
  • 80