Is there any class in the .NET framework that can read/write standard .ini files:
[Section]
=
...
Delphi has th
You should read and write data from xml files since you can save a whole object to xml and also you can populate a object from a saved xml. It is better an easy to manipulate objects.
Here is how to do it: Write Object Data to an XML File: https://msdn.microsoft.com/en-us/library/ms172873.aspx Read Object Data from an XML File: https://msdn.microsoft.com/en-us/library/ms172872.aspx
The creators of the .NET framework want you to use XML-based config files, rather than INI files. So no, there is no built-in mechanism for reading them.
There are third party solutions available, though.
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.
PeanutButter.INI is a Nuget-packaged class for INI files manipulation. It supports read/write, including comments – your comments are preserved on write. It appears to be reasonably popular, is tested and easy to use. It's also totally free and open-source.
Disclaimer: I am the author of PeanutButter.INI.