forked from zhangqi-ulua/XlsxToLua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxtConfigReader.cs
53 lines (47 loc) · 1.84 KB
/
TxtConfigReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections.Generic;
using System.IO;
using System.Text;
public class TxtConfigReader
{
/// <summary>
/// 将文本文件中的键值对读取并加入Dictionary
/// </summary>
public static Dictionary<string, string> ParseTxtConfigFile(string filePath, string separator, out string errorString)
{
if (!File.Exists(filePath))
{
errorString = string.Format("错误:TxtConfig文件不存在,输入路径为{0}", filePath);
return null;
}
Dictionary<string, string> result = new Dictionary<string, string>();
using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
string line = null;
int lineNumber = 0;
while ((line = reader.ReadLine()) != null)
{
++lineNumber;
// 以#开头的注释行和空行忽略
if (string.IsNullOrEmpty(line) || line.StartsWith("#"))
continue;
// 找到key、value
int separatorIndex = line.IndexOf(separator);
if (separatorIndex == -1)
{
errorString = string.Format("错误:第{0}行不合法不包含分隔符{1},文件为{2}", lineNumber, separator, filePath);
return null;
}
string key = line.Substring(0, separatorIndex);
string value = line.Substring(separatorIndex + 1);
if (result.ContainsKey(key))
{
errorString = string.Format("错误:第{0}行中的key({1})重复定义,文件为{2}", lineNumber, key, filePath);
return null;
}
result.Add(key, value);
}
}
errorString = null;
return result;
}
}