Reading ini file in C#
Solution 1
Well, you could 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 points out 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 would be
Dictionary<string, string> ini = splitCache.ToDictionary(
entry => entry.Substring(0, entry.FirstIndexOf("="),
entry => entry.Substring(entry.FirstIndexOf("="));
Solution 2
What is wrong with iterating?
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]);
}
Solution 3
There's actually a Windows API for reading/writing INI files in kernel32.dll
; see this CodeProject article for an example.
Solution 4
Try like this
[DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileString")]
public static extern int GetPrivateProfileString(string SectionName, string KeyName, string Default, StringBuilder Return_StringBuilder_Name, int Size, string FileName);
and call the function like this
GetPrivateProfileString(Section_Name, "SETTING", "0", StringBuilder_Name, 10, "filename.ini");
Value can be accessed from StringBuilder_Name
.
Solution 5
INI files are a bit tricky so I wouldn't recommend rolling your own. I wrote Nini which is a configuration library that includes a very fast parser.
Sample INI file:
; This is a comment
[My Section]
key 1 = value 1
key 2 = value 2
[Pets]
dog = rover
cat = muffy
Same C# code:
// 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");
Paul Michaels
I've been a programmer for most of my life. I have an interest in games, mobile and tablet development, along with message queuing, and pretty much anything that provides an elegant solution to a problem, technical or otherwise. I like learning new technology and finding new ways to use the old. I blog about my experiences here. You can read about me, or contact me on Linked in here.
Updated on June 14, 2022Comments
-
Paul Michaels almost 2 years
I am trying to read an ini file that 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 into a dictionary. My question is, is there a way to do this without iterating through the entire array and manually populating the dictionary?