Reading/writing an INI file

后端 未结 16 2376
南旧
南旧 2020-11-22 00:15

Is there any class in the .NET framework that can read/write standard .ini files:

[Section]
=
...

Delphi has th

相关标签:
16条回答
  • 2020-11-22 01:04

    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

    0 讨论(0)
  • 2020-11-22 01:06

    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.

    • INI handlers can be obtained as NuGet packages, such as INI Parser.
    • You can write your own INI handler, which is the old-school, laborious way. It gives you more control over the implementation, which you can use for bad or good. See e.g. an INI file handling class using C#, P/Invoke and Win32.
    0 讨论(0)
  • 2020-11-22 01:10

    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.

    0 讨论(0)
  • 2020-11-22 01:10

    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.

    0 讨论(0)
提交回复
热议问题