zoukankan      html  css  js  c++  java
  • Using Nini .NET Configuration Library 武胜

    When developing a desktop application, there will be times when you want to store settings for your program. A database is one option, but on Windows, you might just wish to have your settings stored in an INI file. One way to work with an INI file in C# is with the Nini Library. This makes it quite easy to read from and write to an INI file.

    Let’s get started.

    After installing the library, we’ll need to set our namespace.

    What will our INI file look like? Something like this:

    For my application, I decided to make a class devoted to the configuration file. So, let’s define that and a few other variables.

    Now that we have our variables declared, let’s create a couple of useful methods.

    These two methods will update the INI file with new settings, should we change them in our program. Of course, if we make these changes, they need to be saved. Thankfully, we can declare something in our constructor (which we will write a little later) that will auto-save our changes as we make them.

    Now, let’s create a pair of methods to return the data. This will be useful in our program when we need to use these settings.

    With these methods, we now have a basic class for handling a configuration file. All that is left is our constructor.

    But before we get to the constructor, there is something else I created. What if our INI file doesn’t exist? I decided that I would make a function to create a default INI file, should the old one not exist anymore. This is also useful if we want to distribute our program without an INI file.

    We can do a check when we initialize our class that will check to see whether or not this file exists. If not, we’ll create it so we can work with it.

    That makes this our constructor:

    Our whole class thus looks like this:

    That’s how simple it can be to work with your own INI files in C#.

    Did you find this useful? Let me know in the comments!

    xml

    XML Files

    Nini has it's own XML configuration file structure. It provides more flexibility than does the .NET configuration file format. It's main advantages are that you can have more than one XML configuration file and that the format is much more concise. Here is an example of the format. You will notice that it resembles an INI file quite closely. The configuration values are the same as the INI in the previous examples:

    <!-- MyApp.xml -->
    <Nini>
        <Section Name="Logging">
            <Key Name="File Name" Value="MyApp.log" />
            <Key Name="MessageColumns" Value="5" />
            <Key Name="MaxFileSize" Value="40000000000000" />
        </Section>
    </Nini>

    To load the file is very simple:

    // Loads the XML file
    XmlConfigSource source = new XmlConfigSource("MyApp.xml");
    // Retrieves a value
    long maxFileSize = source.Configs["Logging"].GetLong("MaxFileSize");
  • 相关阅读:
    Sublime Text3下使用Python,REPL的安装与快捷键设置方法
    2018最新版本Sublime Text3注册码(仅供测试交流使用)
    Simple website approach using a Headless CMS: Part 3
    Simple website approach using a Headless CMS: Part 2
    Simple Website Approach Using a Headless CMS: Part 1
    Top 19 Headless CMS for Modern Publishers
    Headless CMS
    12位至今仍在世的重要工程师,让我们来认识这些程序界的大前辈
    Linux操作系统(第二版)(RHEL 8/CentOS 8)—内容简介—前言—清华大学出版社—张同光
    List of open source real-time operating systems
  • 原文地址:https://www.cnblogs.com/zeroone/p/2541309.html
Copyright © 2011-2022 走看看