I'm trying to write a small game in VisualStudio, C# language to improve and practice. The thing I'm trying to do is to have a config file that the user can edit with the settings menu in the game. I know I could write a simple .ini file and parse that when the program starts, but is there a better and simpler way to do it?
-
1[Best practice to save application settings in a Windows Forms Application](https://stackoverflow.com/q/453161/1417185) – Paritosh Apr 22 '18 at 10:17
-
[Storing and Retrieving Settings from XML](https://www.codeproject.com/Articles/30834/Storing-and-Retrieving-Settings-from-XML) – Paritosh Apr 22 '18 at 10:18
-
One thing to ask is: do you want users to easily be able to edit those config files manually? ini is generally seen as outdated, but that's usually the criterion to use to decide whether to use it. json and xml have syntaxes that are rather prone to errors if manual editing is a realistic possibility. If the user has no business tweaking it, by all means, go for xml or json. – Nyerguds Apr 22 '18 at 11:43
3 Answers
Just serialize/deserialize the whole class representing your settings to/from a file, that's the easiest to implement. You can use e.g. Json.NET
File.WriteAllText(@"C:\config.json", JsonConvert.SerializeObject(settings));
var settings = JsonConvert.DeserializeObject<Settings>(File.ReadAllText(@"C:\config.json"));

- 916
- 4
- 12
1- Add an Application Configuration File to your project (right click project > add item). This will be called app.config inside your project. 2- Add Parameter to the file by :
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
config.AppSettings.Settings.Add("Yourkey","Value");
config.Save(ConfigurationSaveMode.Minimal);
3 - Retrieve Data:
Configuration config =
ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
string Value = config.AppSettings.Settings["Yourkey"].Value

- 1,075
- 3
- 12
- 38
-
1Thanks, it works, but it doesn't save it to disk, If I run the add part, then comment it out and try to retrive the data, its empty. How can I write it into file, so it remembers your settings, like it should :P – Drake Apr 22 '18 at 11:35
You can use a the integrated settings functionality in .NET Framework in the System.Configuration namespace. To create a settings file, you need to add one in visual studio via the add element dialog. Select "Settings" an give it a name. Add settings file to yout project
Then you need to add settings to the file. There is a nice gui editor when you doble click on the file in solution explorer: Add settings to file
To access the values in code, you need get an instace of the settings object via the static propertie calles "Default" in you settings class: Access settings value in code
Don´t forget to call MySettingsClass.Default.Save()
to save changed values before you close your application.
There are much more features than read an save values withe this method. You can read more about it here and here.

- 51
- 1
- 5