1

Possible Duplicate:
Reading/writing INI file in C#

What methods or C# libraries are available that can do something as below ? Thanks

   //Delphi read item in a given  section of a binary file.
   uses iniFiles; 
   readString('SectionName','itemName','DefaultValueIfNotFound');
Community
  • 1
  • 1
  • use the System.Configuration class – MethodMan Dec 08 '11 at 18:32
  • 2
    When you move to .NET, better leave *.ini behind. – H H Dec 08 '11 at 18:34
  • using app.config and web.config file will allow for more capability, power, and control you can even Encrypt portions of the app.config file it's not very hard to learn the use.. I have coded Delphi for 17 years and I picked up .NET very easy. keep in mind the guy that wrote C#.NET for Microsoft wrote Delphi for Borland, so you should be able to adapt to the cross over very rapidly. – MethodMan Dec 08 '11 at 18:37
  • 1
    Just a note: Your comment references a "binary" file, but an INI file isn't "binary" (it's text). Also, your code fragment is totally invalid; you don't declare or create an instance of `TIniFile` or `TMemIniFile`, nor do you reference one in the call to `ReadString`. – Ken White Dec 08 '11 at 18:52
  • I was ironically wondering the exact same subject just before I saw this, but I had every plan to investigate Google before asking any questions. Also, INI files are not Delphi specific, so the real question is how to use INI files with C#. – Jerry Dodge Dec 08 '11 at 20:30

3 Answers3

9

There aren't classes in the framework for reading INI files directly, as they are not really considered a good option now. Instead, it's typically recommended that you use the Application Settings infrastructure or other more flexible techniques, such as saving information in XML files or databases.

However, many people have written their own libraries, such as this one on CodeProject, which would allow you to easily read values from a section of an ini file in C#.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • +1. Here's another possible CodeProject link [An INI file handling class in C#](http://www.codeproject.com/KB/cs/cs_ini.aspx) – Ken White Dec 08 '11 at 19:02
3

If you really must then use this pinvoke method.

http://pinvoke.net/default.aspx/kernel32.GetPrivateProfileString

INI files are old legacy and not recommended for use by Microsoft.

Mesh
  • 6,262
  • 5
  • 34
  • 53
1

While not exactly the same as using Delphi's TIniFile, I've found the open-source Nini library to work well.

Here's an example of reading an INI file using Nini:

; MyApp.ini
[Logging]
File Name = MyApp.log
MessageColumns = 5
MaxFileSize = 40000000000000

Below is a C# example piece of code that describes how to access the configuration data from the INI file from the file above:

using Nini.Config;
IConfigSource source = new IniConfigSource("MyApp.ini");

string fileName = source.Configs["Logging"].Get("File Name");
int columns = source.Configs["Logging"].GetInt("MessageColumns");
long fileSize = source.Configs["Logging"].GetLong("MaxFileSize");
Mick
  • 13,248
  • 9
  • 69
  • 119