Read the ini file that doesn't have a section?

There is no section in my ini file. It has the following data / toolboxvisibleChild = false / startBroker = false / startWebContainer = true

I want to use a function.

    private static extern int GetPrivateProfileString(string section,
             string key,string def, StringBuilder retVal,
        int size,string filePath);


My problems

  • I cannot name the section name in the function because I do not have
  • If I give the section name null it returns nothing
  • I don't want to use brute force like ReadAllText

source to share

2 answers

Here is the library that the author says supports section keys. I have not tried this library myself.
Or you can just edit the Ini file and add the title / section name at the top right and then delete it once you're done reading.



Using File.ReadLines , and some LINQs are actually not that bad:

var dict = File.ReadLines("config.txt")
               .Where(line => !string.IsNullOrWhiteSpace(line))
               .Select(line => line.Split(new char[] { '=' }, 2, 0))
               .ToDictionary(parts => parts[0], parts => parts[1]);

var result = dict[""];




All Articles