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!

最新文章

  1. python下如何安装biopython
  2. 6.openssl rsautl和openssl pkeyutl
  3. MySql和SQL Server数据类型 对比
  4. php检测php.ini是否配制正确
  5. [转] - 如何用QTcpSocket传送图片
  6. linux fork函数与vfork函数,exit,_exit区别
  7. Java中怎样由枚举常量的ordinal值获得枚举常量对象
  8. 8个3D视觉效果的HTML5动画欣赏
  9. Linux FTP服务安装和远程登录失败
  10. OC语法1——OC概述
  11. 4年前端、2年CTO:一个非科班程序员的真实奋斗史
  12. mysql 字符串去掉指定字符
  13. Java程序生成一个Access文件
  14. React Native不同设备分辨率适配和设计稿尺寸单位px的适配
  15. 9.视差特效、回弹动画、overScrollBy
  16. python中,print函数的sep和end参数
  17. 030_CORS深究
  18. 【Redis】5、Redis事务处理
  19. Spring Java-based容器配置
  20. RetinaNet论文理解

热门文章

  1. 最新的 cocoapods 安装与使用(2016.11)
  2. MarkDown 格式生产类型
  3. SGU 280.Trade centers(贪心)
  4. Android MVP模式
  5. input多选图片与显示
  6. PHP学习心得(五)——类型
  7. netbeans git 配置(ssh方式)
  8. whoosh使用笔记
  9. 学习python网络数据采集笔记-1、2章
  10. Djang DJANGO_SETTINGS_MODULE