tags:

views:

12464

answers:

6

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#.

+22  A: 

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/

David Arno
+16  A: 

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.

splattne
I want to read complete INI file. How to do the same instead of reading section,key
sukumar
+5  A: 

You can try Nini

Muxa
Is the website supposed to be blank, or did the Nini creator simply forget to test it with Firefox?!!!???
David Arno
Try http://sourceforge.net/projects/nini
Patman
@Patman, thanks for the link.
David Arno
+1  A: 

You can use NIni lib.

Kaveh Shahbazian
+2  A: 

There is an Ini Parser available in CommonLibrary.NET

This has various very convenient overloads for getting sections/values and is very light weight.

james
In case it's not obvious from looking at the top level of the library (it wasn't obvious to me!), the IniDcoument class et al are in ComLib.IO.
Tim Keating
+2  A: 

http://code.google.com/p/ini-parser/

is better more simple solution.

VOX