Save settings in .NET desktop program using custom XML configuration files

This article will detail how to use a custom XML configuration file in a .NET desktop program to save and read settings. In addition to XML, we'll also explore other common configuration file formats such as JSON, INI, and YAML, along with their pros and cons and associated NuGet libraries. Finally, we will highlight why we chose XML as the configuration file format and show a practical example.

1. Background

In .NET desktop programs, setting files are usually used to save the program's configuration information. The method of using the setting file is very simple. Just add a setting file to the project, and then obtain and save the settings through Properties.Settings.Default.

My open source project HackerScreenSaver used this method before, but when I upgraded the kernel version of Windows 11, I found that when the program was started as a screensaver by the system, it could not load the configuration.

In this case, you need to use other methods to store configuration files. After all, settings files are not the only way to save settings. We can choose to store it in the registry, or write the configuration file ourselves and save it.

2. Common configuration file formats

For some simple configurations, we can even write a binary file directly. Of course, in general, we still choose configuration files in common formats, such as JSON, INI and YAML.

  1. JSON (JavaScript Object Notation) format
    • Advantages: easy to read and write, supports complex data structures, widely used in Web API and front-end development.
    • Disadvantages: According to the JSON specification, it does not support comments (independent Json files can correctly parse comments in some editors, and when reading configuration through the JSON configuration provider in .NET Core, comments can also be added to the configuration file) .
    • Related class libraries: Newtonsoft.Json(It is recommended to use the built-in ones System.Text.Json) For migration, please refer to the relevant tutorials .
  2. INI(Initialization File)
    • Advantages: concise, easy to read and edit, suitable for storing simple key-value pairs.
    • Disadvantages: Does not support complex data structures and types.
    • Related libraries:ini-parser
  3. YAML(Yet Another Markup Language)
    • Advantages: concise, easy to read and edit, supports complex data structures, supports comments.
    • Disadvantages: Indentation sensitive, may lead to errors.
    • Related libraries:YamlDotNet

3. Configuration options for HackerScreenSaver

In order to keep the design of HackerScreenSaver compact, and based on the principle of appropriateness and simplicity, I finally chose the XML format configuration file that does not require the introduction of other third parties.

XML (eXtensible Markup Language) has the following advantages:

  • It has strong scalability and supports complex data structures.
  • Supports comments for easy understanding and maintenance.
  • As a standard data exchange format, it is easy to integrate with other systems.

The following is a simple configuration class we designed SimpleSettingto save settings:

public class SimpleSetting
{
    
    
    // 类的属性和构造函数省略...

    /// <summary>
    /// 从文件中读取设置
    /// </summary>
    /// <param name="path">配置文件路径</param>
    public SimpleSetting(string path)
    {
    
    
        System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(SimpleSetting));
        using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
        {
    
    
            SimpleSetting ss = (SimpleSetting)xs.Deserialize(fs);
            isLocal = ss.isLocal;
            uInfo = ss.uInfo;
            Opacity = ss.Opacity;
            autoExit = ss.autoExit;
        }
    }

    /// <summary>
    /// 保存设置到文件
    /// </summary>
    /// <param name="path">配置文件路径</param>
    public void Save(string path)
    {
    
    
        System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(SimpleSetting));
        using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
        {
    
    
            xs.Serialize(fs, this);
        }
    }
}

SimpleSettingThe class contains methods for reading settings from an XML file and saving settings to an XML file. In order to use this class, we only need to create an object in the program SimpleSettingand then call its Saveand Loadmethods. In this way, we can use a custom XML configuration file to save settings in the .NET desktop program.

4. Finally

This article details how to use custom XML configuration files in .NET desktop programs and why XML is chosen as the configuration file format. We also explored other common configuration file formats, such as JSON, INI, and YAML, along with their pros and cons and related NuGet libraries. Hope this article helps you find the right configuration file solution for your project needs!

Guess you like

Origin blog.csdn.net/marin1993/article/details/132850473