Is there a practice of storing user interface settings in a file?

I don’t see a satisfactory answer yet, so I thought I would ask. Are there any recommendations for storing user interface settings in a file?

The scenario is that we have a built-in C # application in which I want to store an array of files / folders along with other random things (flags that need to be used when executing an external command, optional arguments for passing to the selected comparison algorithms, etc. e). Due to the nature of these values, using the app.config built-in methods will not work. So, are there any recommendations when storing semi-complex settings in the settings file?

Currently, the most difficult one I keep is a single-entry list that requires storing the file name, comparison algorithm and argument text. Thus, this is not all simple key / value data.

I am looking for thoughts about:

  • (e.g. if XML, values ​​should be in attributes vs node.value)
  • format (XML, YAML, etc.)
  • framework

This will not store sensitive data, so security is not a concern.

Currently, I want to just use an XmlDocument and separate classes for every other collection. But I feel that some existing frameworks already exist for this.

Thanks!

+7
user-interface c # settings configuration-files
source share
2 answers

You can also define a custom configuration section, and then put the settings in a separate configuration file if you don't want to clutter your app.config. A custom configuration module allows you to define the structure for your own complex parameters and save them in a standard configuration file. If you used Entity Framework, corporate library or the like, you probably saw that they are defined at the top of the file. A quick google search opened another StackOverflow post on this subject.

How to create a custom configuration section in app.config

+2
source share

You are correct that such settings should not be stored in the app.config file. It just adds a mess there. In this case, it would be better to create another configuration file, as you mentioned in your ticket.

You plan to structure this configuration file as an XML document. This is a great idea. It also means that your configuration data is structured. This is also great.

I would advise you to go to JSON. This will allow you to load configuration settings into your application at run time with very few lines of code. Try NewtonSoft.JSON (aka. JSON.Net).

You can follow these steps to quickly add and use configuration options:

  • Install NewtonSoft.JSON using the Nuget Package Manager
  • Add a link to this library to your project and a .cs file (using NewtonSoft.Json)
  • Create a C # class for your configuration settings. To repeat blocks, you can use List<string> or any other appropriate data type, even int - so you also do not need to do type conversions.
  • Reading a JSON file into a string object
  • Use JsonConvert.DeserializeObject() as:

MyConfigSettings settings = JsonConvert.DeserializeObject<MyConfigSettings>(jsonString);

Here MyConfigSettings is a class of configuration settings. Corresponding members must be declared as public properties with corresponding get; set; attributes get; set; get; set; .

This will load all configuration settings into the settings object. The best part is that any missing configuration values ​​will be automatically skipped, so you don’t have to have complicated parsing algorithms.

To write configuration values ​​to a JSON file, you can fill in the values ​​in the settings object and serialize them using:

 // Indented so that it looks readable when the config file is opened in editor like Notepad string data = JsonConvert.SerializeObject<MyConfigSettings>(settings, Formatting.Indented); // Now just store string object data to the config file 

Note: This will require .Net 4.0 +

A quick start is given in this example .

+9
source share

All Articles