@manhng

Welcome to my blog!

Read/Write settings to INI File using C#

June 12, 2020 22:03

Read/Write settings to INI File using C# (edit)

https://github.com/rickyah/ini-parser

https://csharp.hotexamples.com/examples/IniParser/FileIniDataParser/-/php-fileinidataparser-class-examples.html

Config.ini

[Group]
Data Source=localhost
Initial Catalog=Northwind
UserId=sa
Password=123456
ConnectionTimeout=15
SQLCommandTimeout=30

Code Sample

using IniParser;
using IniParser.Model;
using System;
using System.Windows.Forms;

namespace MyNamespace
{
public partial class Form1 : Form
{
private static string m_BasePath = string.Empty;
private static string m_BaseDir = string.Empty;

public Form1()
{
InitializeComponent();
}

static Form1()
{
m_BasePath = Application.StartupPath;
m_BaseDir = AppDomain.CurrentDomain.BaseDirectory;
}

private void Form1_Load(object sender, EventArgs e)
{
string iniFilePath = System.IO.Path.Combine(m_BaseDir, "config.ini");
FileIniDataParser i = new FileIniDataParser();
i.Parser.Configuration.CommentString = "#";
IniData data = i.ReadFile(iniFilePath);
txtDataSource.Text = data["Group"]["Data Source"];
txtInitialCatalog.Text = data["Group"]["Initial Catalog"];
txtUserId.Text = data["Group"]["UserId"];
txtPassword.Text = data["Group"]["Password"];
txtConnectionTimeout.Text = data["Group"]["ConnectionTimeout"];
txtSQLCommandTimeout.Text = data["Group"]["SQLCommandTimeout"];
}
}
}

Code Sample

abc 

Categories

Recent posts