I have a file with some key pair values
key1 = value1
key2 = value2
[section name]
key3 = value3
key4 = value4
so I don’t care about section names as the keys are unique. I just want to get the the value for an input key. I have the following to read the lines.
var userDataLines = File.ReadAllLines(pathToFile);
and I have something like this
var result = userDataLines.Select(userDataLine => userDataLine.Split(new[] { '=' }))
.Where(split => split.Length == 2);
gives me all the key value pairs in one collection.
but essentially I want to get a dictionary with keys and values from my file but not sure how to do that. Can anyone poing me in the right direction?
Thanks
You can use the ToDictionary Extension Method as follows:
(Small improvements: use ReadLines instead of ReadAllLines, and split each line into at most 2 parts.)