Read ini file which does not have any section?

2019-05-15 08:38发布

My ini file does not have any section. It has following data

com.ibm.rcp.toolbox.admin/toolboxvisibleChild=false
com.ibm.collaboration.realtime.community/defaultAuthType=ST-DOMINO-SSO
com.ibm.collaboration.realtime.brokerbridge/startBroker=false
com.ibm.collaboration.realtime.webapi/startWebContainer=true

I want to use function.

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
             string key,string def, StringBuilder retVal,
        int size,string filePath);

My problems

  1. I cannot give section name in the function because I dont have any
  2. If I give section name null, it returns nothing
  3. I don't want to use brute force like ReadAllText

标签: c# .net-4.0 ini
2条回答
Ridiculous、
2楼-- · 2019-05-15 09:04

Here's a library that the author says supports section-less keys. I myself have not tried this library.
Or, you could simply edit the Ini file and add in a "header"/section name right at the top, then delete it once you're done reading.

查看更多
Root(大扎)
3楼-- · 2019-05-15 09:09

Using File.ReadLines and some LINQ is 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["com.ibm.rcp.toolbox.admin/toolboxvisibleChild"];
查看更多
登录 后发表回答