目录
介绍
使用代码
下载源代码 - 5.7 KB
介绍有时,我需要阅读INI文件,通常作为一个更大项目的一小部分。因此,我真的不希望它需要一堆基础设施或其他“买入”。我只想要一些简单的东西,可以让我得到我需要的东西,而不用大惊小怪。那里有很多INI阅读器代码,但很多都是镀金的,所以这不是我需要的。这很简单,您可以复制并粘贴到您的项目中。
使用代码INI文件通常分节布局,每个节下都有键值对。此INI阅读器会将多个具有相同名称的部分以及同一部分下的多个键组合为具有多个值的单个键。它还允许您为每一行的一个键指定值。默认部分是空字符串。
有两种感兴趣的方法。第一个用于读取INI文件:
var ini = Ini.Read(myTextReader);
调用者负责关闭阅读器。该ini变量现在包含一种IDictionary类型。这是一种有点麻烦的类型,但使用它非常简单。
INI文件中的每个[section]都是外部字典中的一个键。每个key = value对都成为该部分条目的内部字典下的字典条目。如果键只有一个值,则字典值将是string。如果每个键有多个值,则该键的值将是IList。
至于另一种方法,您可以使用Ini.ToString(ini)将结果转储为字符串。
如果您只想从此处粘贴它,这里是完整的相关代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
static class Ini {
static void AddIniEntryValue(IDictionary d, string line,
string name, int i, object o) {
if (o is string) {
var s = (string)o;
d.Remove(name);
var col = new List();
d.Add(name, col);
col.Add(s);
col.Add(line.Substring(i + 1).TrimStart());
} else {
((List)o).Add(line.Substring(i + 1).TrimStart());
}
}
///
/// Reads an INI file as a nested dictionary.
/// The outer dictionary contains a dictionary for each section.
/// The inner dictionary contains a name, and a string or a list of strings
/// when an entry has multiple items.
///
/// The text reader
/// The comparer to use for keys.
/// Defaults to culture invariant and case insensitive.
/// A nested dictionary
public static IDictionary
Read(TextReader reader, IEqualityComparer comparer = null) {
if (comparer == null) {
comparer = StringComparer.InvariantCultureIgnoreCase;
}
int lc = 1;
var result = new Dictionary(comparer);
string section = "";
string name = null;
string line;
while (null != (line = reader.ReadLine())) {
var i = line.IndexOf(';');
if (i > -1) {
line = line.Substring(0, i);
}
line = line.Trim();
if (!string.IsNullOrEmpty(line)) {
IDictionary d;
if (line.Length > 2 && line[0] == '[' && line[line.Length - 1] == ']') {
section = line.Substring(1, line.Length - 2);
if (!result.TryGetValue(section, out d)) {
d = new Dictionary(comparer);
result.Add(section, d);
}
} else {
d = result[section];
i = line.IndexOf('=');
if (i > -1) {
name = line.Substring(0, i).TrimEnd();
object o;
if (d.TryGetValue(name, out o)) {
AddIniEntryValue(d, line, name, i, o);
} else
d.Add(name, line.Substring(i + 1).TrimStart());
} else if (null == name) {
throw new IOException("Invalid INI file at line " + lc.ToString());
} else {
var o = d[name];
AddIniEntryValue(d, line, name, i, o);
}
}
}
++lc;
}
return result;
}
public static string ToString(IDictionary ini) {
var sb = new StringBuilder();
foreach (var sentry in ini) {
sb.AppendLine("[" + sentry.Key + "]");
var d = sentry.Value;
foreach (var entry in d) {
if (entry.Value is IList) {
var l = ((IList)entry.Value);
sb.AppendLine(string.Format("{0} = {1}", entry.Key, l[0]));
for (var i = 1; i < l.Count; ++i) {
sb.AppendLine("\t" + l[i]);
}
sb.AppendLine();
} else
sb.AppendLine(string.Format("{0} = {1}", entry.Key, entry.Value));
}
}
return sb.ToString();
}
}
https://www.codeproject.com/Tips/5319923/IniReader-A-Simple-Tiny-INI-Reader