Is there any class in the .NET framework that can read/write standard .ini files:
[Section]
<keyname>=<value>
...
Delphi has the TIniFile
component and I want to know if there is anything similar for C#?
cininet
Is there any class in the .NET framework that can read/write standard .ini files:
[Section]
<keyname>=<value>
...
Delphi has the TIniFile
component and I want to know if there is anything similar for C#?
Best Answer
Preface
Firstly, read this MSDN blog post on the limitations of INI files. If it suits your needs, read on.
This is a concise implementation I wrote, utilising the original Windows P/Invoke, so it is supported by all versions of Windows with .NET installed, (i.e. Windows 98 - Windows 10). I hereby release it into the public domain - you're free to use it commercially without attribution.
The tiny class
Add a new class called
IniFile.cs
to your project:How to use it
Open the INI file in one of the 3 following ways:
You can write some values like so:
To create a file like this:
To read the values out of the INI file:
Optionally, you can set
[Section]
's:To create a file like this:
You can also check for the existence of a key like so:
You can delete a key like so:
You can also delete a whole section (including all keys) like so:
Please feel free to comment with any improvements!