Ads

Get STM32 tutorial using HAL at $10 for a limited time!

Wednesday, June 17, 2015

Using INI File for Storing Application Settings on .NET C#

In this post, I will explain INI file for storing application settings such as database connection settings. INI files are simple text file which have a basic structure of sections, properties, and values. INI file have filename extension .ini, which stands for "initialization". Every property has a value delimited by an equal sign.
property=value
Properties can be grouped into sections. The sections name appear inside square brackets. Sections is not necessary needed on every INI file.
[section]
property=value
Every line that started with a semicolon is a comment.
;This is a comment
This is a sample of database connection settings file using INI file:
;Database connection settings example
[database_connection]
host=127.0.0.1
port=5432
name=db_test
user=postgres
pass=postgres
To open this INI file on .NET, we need INI file parsing library. I am use this library for parsing INI file. After download this library, we must INIFileParser.dll add to reference then add this include code:
using IniParser;
This code below is explain how to load data from INI file and save data to INI file:
// Create INI parser object
FileIniDataParser parser = new FileIniDataParser();

// Load INI file
IniData data = parser.LoadFile("settings.ini");

// Get value from property "host" inside 
// a section named "database_connection"
string host = data["database_connection"]["host"];

// Change host value to "192.168.1.168"
data["database_connection"]["host"] = "192.168.1.168";

// Save INI file
parser.SaveFile("settings.ini", data);

No comments :

Post a Comment