.properties风格的配置文件
最近忙着Pocket PC上的.net应用开发,.config xml格式的配置文件不好用,挺欣赏java中常用的.properties风格的配置文件,顺手写了PropertiesFileHelper。
1/// <summary>
2/// .properties配置文件解析帮助类.
3/// </summary>
4public sealed class PropertiesFileHelper
5{
6 /// <summary>
7 /// <value>缺省的注释符号.</value>
8 /// </summary>
9 public static readonly string DEAFULT_NOTE_DENOTATION = @"#";
10
11 /// <summary>
12 /// <value>缺省的key/value分隔符.</value>
13 /// </summary>
14 public static readonly string DEFAULT_DELIMITER = @"=";
15
16 private PropertiesFileHelper()
17 {
18 }
19
20 /// <summary>
21 /// 解释一个.properties的配置文件.
22 /// </summary>
23 /// <param name="filePath">文件路径.</param>
24 /// <returns>key/value pair table.</returns>
25 public static Hashtable ParsePropertiesFile(string filePath)
26 {
27 if (!File.Exists(filePath))
28 {
29 throw new FileNotFoundException(filePath);
30 }
31
32 Hashtable _keyValues = new Hashtable();
33
34 string textLine;
35 using (StreamReader sr = new StreamReader(filePath))
36 {
37 while (sr.Peek() >= 0)
38 {
39 textLine = sr.ReadLine();
40 if (textLine.Trim().Length > 0)
41 {
42 ParsePropertiesLine(_keyValues, textLine);
43 }
44 }
45 }
46
47 return _keyValues;
48 }
49
50 private static void ParsePropertiesLine(Hashtable keyValues, string textLine)
51 {
52 if (string.IsNullOrEmpty(textLine))
53 {
54 return;
55 }
56
57 // 注释.
58 if (textLine.StartsWith(DEAFULT_NOTE_DENOTATION))
59 {
60 return;
61 }
62
63 int pos = textLine.IndexOf(DEFAULT_DELIMITER);
64 if (pos > 0)
65 {
66 keyValues.Add(textLine.Substring(0, pos).Trim(), textLine.Substring(pos + DEFAULT_DELIMITER.Length, textLine.Length - pos -DEFAULT_DELIMITER.Length));
67 }
68 }
69}
2/// .properties配置文件解析帮助类.
3/// </summary>
4public sealed class PropertiesFileHelper
5{
6 /// <summary>
7 /// <value>缺省的注释符号.</value>
8 /// </summary>
9 public static readonly string DEAFULT_NOTE_DENOTATION = @"#";
10
11 /// <summary>
12 /// <value>缺省的key/value分隔符.</value>
13 /// </summary>
14 public static readonly string DEFAULT_DELIMITER = @"=";
15
16 private PropertiesFileHelper()
17 {
18 }
19
20 /// <summary>
21 /// 解释一个.properties的配置文件.
22 /// </summary>
23 /// <param name="filePath">文件路径.</param>
24 /// <returns>key/value pair table.</returns>
25 public static Hashtable ParsePropertiesFile(string filePath)
26 {
27 if (!File.Exists(filePath))
28 {
29 throw new FileNotFoundException(filePath);
30 }
31
32 Hashtable _keyValues = new Hashtable();
33
34 string textLine;
35 using (StreamReader sr = new StreamReader(filePath))
36 {
37 while (sr.Peek() >= 0)
38 {
39 textLine = sr.ReadLine();
40 if (textLine.Trim().Length > 0)
41 {
42 ParsePropertiesLine(_keyValues, textLine);
43 }
44 }
45 }
46
47 return _keyValues;
48 }
49
50 private static void ParsePropertiesLine(Hashtable keyValues, string textLine)
51 {
52 if (string.IsNullOrEmpty(textLine))
53 {
54 return;
55 }
56
57 // 注释.
58 if (textLine.StartsWith(DEAFULT_NOTE_DENOTATION))
59 {
60 return;
61 }
62
63 int pos = textLine.IndexOf(DEFAULT_DELIMITER);
64 if (pos > 0)
65 {
66 keyValues.Add(textLine.Substring(0, pos).Trim(), textLine.Substring(pos + DEFAULT_DELIMITER.Length, textLine.Length - pos -DEFAULT_DELIMITER.Length));
67 }
68 }
69}