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.

using Nini.Config;
1
using Nini.Config;

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

 
;conf.ini
[Options]
Zipped = 0
Filename = test.txt
1
2
3
4
;conf.ini
[Options]
Zipped=0
Filename=test.txt

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

 
public class OurConfig
{
string NL = Environment.NewLine; // New line character
private string configFile = "conf.ini"; // Our INI file

IConfigSource config; // Instance of our config

}

1
2
3
4
5
6
7
8
publicclassOurConfig
{
stringNL=Environment.NewLine;// New line character
privatestringconfigFile="conf.ini";// Our INI file
 
IConfigSource config;// Instance of our config
 
}

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

public void set_is_zip(int zipped)
{
config.Configs["Options"].Set("Zipped", zipped);
}

public void set_filename(string fname)
{
config.Configs["Options"].Set("Filename", fname);
}

1
2
3
4
5
6
7
8
9
publicvoidset_is_zip(intzipped)
{
config.Configs["Options"].Set("Zipped",zipped);
}
 
publicvoidset_filename(stringfname)
{
config.Configs["Options"].Set("Filename",fname);
}

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.

config.AutoSave = true;
1
config.AutoSave=true;

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.

public int return_is_zip()
{
return config.Configs["Options"].Get("Zipped");
}

public string return_filename()
{
return config.Configs["Options"].Get("Filename");
}

1
2
3
4
5
6
7
8
9
publicintreturn_is_zip()
{
returnconfig.Configs["Options"].Get("Zipped");
}
 
publicstringreturn_filename()
{
returnconfig.Configs["Options"].Get("Filename");
}

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.

private void fill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
string toWrite = ";conf.ini" + NL
+ "[Options]" + NL
+ "Zipped = 0" + NL
+ "Filename = test.txt" + NL;

System.IO.File.WriteAllText(@"conf.ini", toWrite);
}

1
2
3
4
5
6
7
8
9
10
11
privatevoidfill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
stringtoWrite=";conf.ini"+NL
+"[Options]"+NL
+"Zipped = 0"+NL
+"Filename = test.txt"+NL;
 
System.IO.File.WriteAllText(@"conf.ini",toWrite);
}

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:

public OurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile = new IniConfigSource("conf.ini");
}
catch (Exception ex)
{
// Write default values into it
fill_new_ini();
configFile = new IniConfigSource("conf.ini");
}

configFile.AutoSave = true; // Auto save config file as we make changes

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
publicOurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile=newIniConfigSource("conf.ini");
}
catch(Exception ex)
{
// Write default values into it
fill_new_ini();
configFile=newIniConfigSource("conf.ini");
}
 
configFile.AutoSave=true;// Auto save config file as we make changes
 
}

Our whole class thus looks like this:

using Nini.Config;

public class OurConfig
{
string NL = Environment.NewLine; // New line character
private string configFile = "conf.ini"; // Our INI file

IConfigSource config; // Instance of our config

public OurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile = new IniConfigSource("conf.ini");
}
catch (Exception ex)
{
// Write default values into it
fill_new_ini();
configFile = new IniConfigSource("conf.ini");
}
configFile.AutoSave = true; // Auto save config file as we make changes
}

private void fill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
string toWrite = ";conf.ini" + NL
+ "[Options]" + NL
+ "Zipped = 0" + CL
+ "Filename = test.txt" + CL;
System.IO.File.WriteAllText(@"conf.ini", toWrite);

}

public void set_is_zip(int zipped)
{
config.Configs["Options"].Set("Zipped", zipped);
}

public void set_filename(string fname)
{
config.Configs["Options"].Set("Filename", fname);
}

public int return_is_zip()
{
return config.Configs["Options"].Get("Zipped");
}

public string return_filename()
{
return config.Configs["Options"].Get("Filename");
}

} // End OurConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Nini.Config;
 
publicclassOurConfig
{
stringNL=Environment.NewLine;// New line character
privatestringconfigFile="conf.ini";// Our INI file
 
IConfigSource config;// Instance of our config
 
publicOurConfig()
{
// Initialize the INI file if it doesn't exist
try
{
configFile=newIniConfigSource("conf.ini");
}
catch(Exception ex)
{
// Write default values into it
fill_new_ini();
configFile=newIniConfigSource("conf.ini");
}
configFile.AutoSave=true;// Auto save config file as we make changes
}
 
privatevoidfill_new_ini()
{
// Put default values into the INI file
// Essentially, we're writing a blank file, so this is fairly simple
stringtoWrite=";conf.ini"+NL
+"[Options]"+NL
+"Zipped = 0"+CL
+"Filename = test.txt"+CL;
System.IO.File.WriteAllText(@"conf.ini",toWrite);
 
}
 
publicvoidset_is_zip(intzipped)
{
config.Configs["Options"].Set("Zipped",zipped);
}
 
publicvoidset_filename(stringfname)
{
config.Configs["Options"].Set("Filename",fname);
}
 
publicintreturn_is_zip()
{
returnconfig.Configs["Options"].Get("Zipped");
}
 
publicstringreturn_filename()
{
returnconfig.Configs["Options"].Get("Filename");
}
 
}// End OurConfig

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!

Using Nini .NET Configuration Library的更多相关文章

  1. Java资源大全中文版(Awesome最新版)

    Awesome系列的Java资源整理.awesome-java 就是akullpp发起维护的Java资源列表,内容包括:构建工具.数据库.框架.模板.安全.代码分析.日志.第三方库.书籍.Java 站 ...

  2. 使用.NET Framework的配置文件app.config

    在一般的项目中,为了使你的代码更加灵活,更方便调整,减少不必要的hard code,我们都在config中添加许多配置信息,一般可以选择.NET自带的配置文件形式app.config或者web项目中的 ...

  3. Awesome C/C++

    Awesome C/C++ A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. In ...

  4. P6 EPPM 16.1 安装和配置指南 1

    安装和配置指南下一topiccontents这些指南解释如何安装和配置数据库服务器,和P6 EPPM,模块:他们还提供在P6 EPPM能够解决所有模块的概述.标准指南帮助您配置和部署应用程序向导P6 ...

  5. Linux下用C读取配置文件。类似ini这样。

    Introduction ccl is the customizable configuration library, a collection of functions for applicatio ...

  6. STM32 使用 printf 发送数据配置方法 -- 串口 UART, JTAG SWO, JLINK RTT

    STM32串口通信中使用printf发送数据配置方法(开发环境 Keil RVMDK) http://home.eeworld.com.cn/my/space-uid-338727-blogid-47 ...

  7. awesome-java

    Awesome Java A curated list of awesome Java frameworks, libraries and software. Awesome Java Ancient ...

  8. awesome cpp

    https://github.com/fffaraz/awesome-cpp Awesome C/C++ A curated list of awesome C/C++ frameworks, lib ...

  9. ASF (0) - ASF Java 项目总览

    Apache .NET Ant Library This is a library of Ant tasks that help developing .NET software. It includ ...

随机推荐

  1. nvidia安装与卸载方式

    第1种方法:.最好的方式不是手动安装官方驱动(手动安装官方驱动无法使用gpu,而且无法启用3d,同时无法生效,所以最好采用此种方法),而是使用bumblebee-nvidia安装,不过要先添加x-sw ...

  2. OC加强-day05

    #program mark - 0_11 NSRange结构体介绍 [掌握] 是Foundation框架中的一个结构体 NSRange 定义的一个变量的两个属性 location:起始下标 lengt ...

  3. Nuage SDN

    Nuage推出纯软件解决方案虚拟化业务平台(VSP)由三部分组成:虚拟化业务目录(VSD).虚拟化业务控制器(VSC)和虚拟路由与交换(VRS). VSD是业务/IT策略引擎,可提供业务模板与分析,每 ...

  4. Cogs 1844. [JSOI2008]最大数maxnumber

    [JSOI2008]最大数maxnumber ★★ 输入文件:bzoj_1012.in 输出文件:bzoj_1012.out 简单对比 时间限制:3 s 内存限制:162 MB [题目描述] 现在请求 ...

  5. Codevs 2449 骑士精神 2005年省队选拔赛四川

    2449 骑士精神 2005年省队选拔赛四川 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **大师 Master** 题目描述 Description 在一个5×5的棋盘上有12 ...

  6. C++模板学习随笔

    本文学习内容参考:http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html C++模板 1.模板分为函数模板和类模板两种类型 函数模板针 ...

  7. Nginx中让 重写后的路径 自动增加斜线 /

    http://www.111cn.net/sys/nginx/56067.htm(参考文章) 现在有个这样的需求,在重写的url地址后,自动加斜线 / 例如 xx.com/abc/1-2  (默认ur ...

  8. Android学习5—布局简介

    Android界面的布局主要有四种,分别为RelativeLayout.LinearLayout.TableLayout.FrameLayout,接下来分别介绍这些布局如何使用(为了简单起见,接下来的 ...

  9. Redis — CentOS6.4安装Redis以及安装PHP客户端phpredis

    一.安装Redis 1.下载安装包 wget http://download.redis.io/releases/redis-2.8.6.tar.gz 2.解压包 tar xzf redis-2.8. ...

  10. oracle工具 sqlplus 用户管理

    可以通过system用户对普通用户解锁.alter user scott account unlock; 工具:1/ sqlplusw sqlplus 自带.调用oracle安装目录下bin目录下的e ...