Reading ini file in c #

I am trying to read an ini file which has the following format:

SETTING=VALUE 
SETTING2=VALUE2

I currently have the following code:

string cache = sr.ReadToEnd();                    
string[] splitCache = cache.Split(new string[] {"\n", "\r\n"}, StringSplitOptions.RemoveEmptyEntries);

Which gives me a list of settings, however what I would like to do is read this in the dictionary. My question is, is there a way to do this without repeating the entire array and manually populating the dictionary?

+3
source share
6 answers

Well, you can use LINQ and do

Dictionary<string, string> ini = (from entry in splitCache
                                  let key = entry.Substring(0, entry.FirstIndexOf("="))
                                  let value = entry.Substring(entry.FirstIndexOf("="))
                                  select new { key, value }).ToDictionary(e => e.key, e => e.value);

As Binary Worrier notes in the comments, this way of doing this has no advantages over the simple loop suggested by the other answers.

Edit: A shorter version of the block above will be

Dictionary<string, string> ini = splitCache.ToDictionary(
                                   entry => entry.Substring(0, entry.FirstIndexOf("="),
                                   entry => entry.Substring(entry.FirstIndexOf("="));
+7
source

What is wrong with iteration?

var lines = File.ReadAllLines("pathtoyourfile.ini");
var dict = new Dictionary<string, string>();

foreach(var s in lines)
{
     var split = s.Split("=");
     dict.Add(split[0], split[1]);
}
+4
source

Windows API / INI kernel32.dll; . CodeProject .

+3

[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]

public static extern int GetPrivateProfileString(string SectionName, string KeyName, string Default, StringBuilder Return_StringBuilder_Name, int Size, string FileName);

,

GetPrivateProfileString(Section_Name, "SETTING", "0", StringBuilder_Name, 10, "filename.ini");

StringBuilder_Name.

+1

INI , . Nini, , .

INI:

; This is a comment
[My Section]
key 1 = value 1
key 2 = value 2

[Pets]
dog = rover
cat = muffy

#:

// Load the file
IniDocument doc = new IniDocument ("test.ini");

// Print the data from the keys
Console.WriteLine ("Key 1: " + doc.Get ("My Section", "key 1"));
Console.WriteLine ("Key 2: " + doc.Get ("Pets", "dog"));

// Create a new section
doc.SetSection ("Movies");

// Set new values in the section
doc.SetKey ("Movies", "horror", "Scream");
doc.SetKey ("Movies", "comedy", "Dumb and Dumber");

// Remove a section or values from a section
doc.RemoveSection ("My Section");
doc.RemoveKey ("Pets", "dog");

// Save the changes
doc.Save("test.ini");
+1

, =?

var dict = new Dictionary<string,string>();
foreach (var line in File.ReadAllLines(filename)) {
  var parts = line.Split('=', 2); // Maximum of 2 parts, so '=' in value ignored.
  dict.Add(parts[0], parts[1]);
}

( .NET 4 ReadAllLines ReadLines, , ReadLines IEnumerable<String> .)

0

Source: https://habr.com/ru/post/1745076/


All Articles