What I want to achieve is very simple: I have a Windows Forms (.NET 3.5) application that uses a path for reading information. This path can be modified by the user, by using the options form I provide.
Now, I want to save the path value to a file for later use. This would be one of the many settings saved to this file. This file would sit directly in the application folder.
I understand three options are available:
- ConfigurationSettings file (appname.exe.config)
- Registry
- Custom XML file
I read that the .NET configuration file is not foreseen for saving values back to it. As for the registry, I would like to get as far away from it as possible.
Does this mean that I should use a custom XML file to save configuration settings? If so, I would like to see code example of that (C#).
I have seen other discussions on this subject, but it is still not clear to me.
Other options, instead of using a custom XML file, we can use a more user friendly file format: JSON or YAML file.
You can store your settings file in multiple special folders (for all users and per user) as listed here Environment.SpecialFolder Enumeration and multiple files (default read only, per role, per user, etc.)
If you choose to use multiple settings, you can merge those settings: For example, merging settings for default + BasicUser + AdminUser. You can use your own rules: the last one overrides the value, etc.
If you work with Visual Studio then it is pretty easy to get persistable settings. Right click on the project in Solution Explorer, choose Properties. Select the Settings tab, click on the hyperlink if settings doesn't exist. Use the Settings tab to create application settings. Visual Studio creates the files
Settings.settings
andSettings.Designer.settings
that contain the singleton classSettings
inherited from ApplicationSettingsBase. You can access this class from your code to read/write application settings:This technique is applicable both for console, Windows Forms and other project types.
Note that you need to set the scope property of your settings. If you select Application scope then Settings.Default.< your property > will be read-only.
"Does this mean that I should use a custom XML file to save configuration settings?" No, not necessarily. We use SharpConfig for such operations.
For instance, if config file is like that
We can retrieve values like this
It is compatible with .Net 2.0 and higher. We can create config files on the fly and we can save it later. Source: http://sharpconfig.net/ Github: https://github.com/cemdervis/SharpConfig
I hope it helps.
A simple way is to use a configuration data object, save it as an XML file with the name of the application in the local Folder and on startup read it back.
Here is an example to store the position and size of a form.
The configuration dataobject is strongly typed and easy to use:
A manager class for saving and loading:
Now you can create an instance and use in your form's load and close events:
And the produced XML file is also readable:
I don't like the proposed solution of using
web.config
orapp.config
. Try reading your own XML. Have a look at XML Settings Files – No more web.config.Sometimes you want to get rid of those settings kept in the traditional web.config or app.config file. You want more fine grained control over the deployment of your settings entries and separated data design. Or the requirement is to enable adding new entries at runtime.
I can imagine two good options:
The advantage of the strongly typed version are the strongly typed settings names and values. There is no risk of intermixing names or data types. The disadvantage is that more settings have to be coded, cannot be added at runtime.
With the object oriented version the advantage is that new settings can be added at runtime. But you do not have strongly typed names and values. Must be careful with string identifiers. Must know data type saved earlier when getting a value.
You can find the code of both fully functional implementations HERE.