Is there any class in .Net framework that can read/write standard ini files:
[Section]
<keyname>=<value>
...
Delphi have TIniFile component and I am looking if there is anything similar for C#.
Is there any class in .Net framework that can read/write standard ini files:
[Section]
<keyname>=<value>
...
Delphi have TIniFile component and I am looking if there is anything similar for C#.
The creators of the .NET framework want you to use XML-based config files, rather than ini files. So no, there is no builtin mechanism for reading them.
There are third party solutions available though. Take a look at:
http://www.codeproject.com/KB/cs/cs_ini.aspx and
http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/
This article on CodeProject "An INI file handling class using C#" should help.
The author created a C# class "Ini" which exposes two functions from KERNEL32.dll. These functions are: WritePrivateProfileString
and GetPrivateProfileString
. You will need two namespaces: System.Runtime.InteropServices
and System.Text
.
Steps to use the Ini class
In your project namespace definition add
using INI;
Create a INIFile like this
INIFile ini = new INIFile("C:\\test.ini");
Use IniWriteValue
to write a new value to a specific key in a section or use IniReadValue
to read a value FROM a key in a specific Section.
Note: if you're beginning from scratch, you could read this MSDN article: How to: Add Application Configuration Files to C# Projects. It's a better way for configuring your application.
There is an Ini Parser available in CommonLibrary.NET
This has various very convenient overloads for getting sections/values and is very light weight.