博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Using Nini .NET Configuration Library
阅读量:6479 次
发布时间:2019-06-23

本文共 7899 字,大约阅读时间需要 26 分钟。

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 . 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:

To load the file is very simple:

// Loads the XML fileXmlConfigSource source = new XmlConfigSource("MyApp.xml");// Retrieves a valuelong maxFileSize = source.Configs["Logging"].GetLong("MaxFileSize");

转载地址:http://ruwuo.baihongyu.com/

你可能感兴趣的文章
Flex布局
查看>>
Material Design之 AppbarLayout 开发实践总结
查看>>
Flutter之MaterialApp使用详解
查看>>
DataBinding最全使用说明
查看>>
原生Js交互之DSBridge
查看>>
Matlab编程之——卷积神经网络CNN代码解析
查看>>
三篇文章了解 TiDB 技术内幕 —— 说计算
查看>>
copy strong weak assign的区别
查看>>
OpenCV 入门
查看>>
css 3D transform变换
查看>>
ele表格合并行之后的selection选中
查看>>
正则表达式分解剖析(一文悟透正则表达式)
查看>>
解决UILable标点符号居中的问题
查看>>
HTML5新特性教程
查看>>
ImageOptim-无损图片压缩Mac版
查看>>
12 Go语言map底层浅析
查看>>
vue-resumer 项目中 element-ui 遇到的 textarea autosize 问题
查看>>
以主干开发作为持续交付的基础
查看>>
PHP扩展库PEAR被攻击,近半年下载者或被影响
查看>>
传统运维团队转型应该注意哪些问题?
查看>>