1

I want to storen and read a number of settings that the user can change. I followed as mentioned here via the properties of the project, but I saw that there is also a file called "Settings.settings". This file is empty. Is this file not needed? Can I delete it?

Another question I have is how to read the settings. For example I have a setting called "InitialSave" as a boolean. Using this gives the error "Operator '==' cannot be applied to operands of type 'object' and 'bool'". Can I read direct from the settings or should I declare each setting at startup?

Properties.Settings.Default["InitialSave"] == true
gunr2171
  • 16,104
  • 25
  • 61
  • 88
lonnebol
  • 67
  • 1
  • 7

2 Answers2

2

You right click this and choose "Open With.. Settings Designer":

enter image description here

Or you get properties of the project and click Settings on the left. Then you see this:

enter image description here

You put some settings and types in:

enter image description here

You write Properties.Settings.Default. in code and then you see your settings:

enter image description here

You can read and write them, as properties of the types you specified

You can even bind them to controls:

enter image description here

Toggling that control tick state would alter the value of Proeprties.Settings.Default.IsMinimized

note: Only User Scoped settings can be changed/saved. App Scope settings are kept in the YourExeName.config file and are changed by editing the file with a text editor. User Scope settings are kept elsewhere (in your user folder) and can be modified and saved at runtime to keep that value for next time the program launches


Edit:

OK, so you said that you can have Settings.settings open in two places; I can't make this occur with the following steps (uses .NET core because Wolle's answer claims core is different; I couldn't support that claim either):

  • New project, .NET Core Winforms:

enter image description here

  • Go to project..settings "The project doesn't contain a settings file, click to create one", et voila:

enter image description here

  • Switch to another tab in project properties, for example:

enter image description here

  • Right click/Open with the settings.settings file. see this:

enter image description here

  • Try to go back to it in project properties, see this:

enter image description here

  • Add some setting:

enter image description here

  • Access it in the code..

enter image description here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Thanks. What is the difference then between Settings.settings and the Settings tab under properties of the project? – lonnebol Feb 01 '21 at 16:38
  • No difference at all. indeed if you open it with right click as I describe then try open it via project proeprties youll get an error "This is already open elsewhere" – Caius Jard Feb 01 '21 at 16:40
  • Well, that's the thing. That is not the case. I can have Setting.settings open and the settings tab under the properties of the project. Under the properties there is already a DatabaseConnectionString. This is not showing in Settings.settings. So they appear not to be the same. – lonnebol Feb 01 '21 at 17:49
  • Something wonky there. Try it again with a new project. See the edit to my answer – Caius Jard Feb 01 '21 at 21:33
0

Unfortunately, @Caius Jard's answer is a little bit outdated, but the go-to way for pre-.NET Core environements.

For the current .NET 5 framework, the most useful source should be the MSDN docs on configuration.

If you want to store your settings in an file, have a look at the file configuration provider.

var appSettings = ConfigurationManager.AppSettings;

// read
var foo = appSettings["bar"];

// write
if (settings[key] == null)  
{  
    settings.Add(key, value);  
}  
else  
{  
    settings[key].Value = value;  
}      
ˈvɔlə
  • 9,204
  • 10
  • 63
  • 89