.properties风格的配置文件

最近忙着Pocket PC上的.net应用开发,.config xml格式的配置文件不好用,挺欣赏java中常用的.properties风格的配置文件,顺手写了PropertiesFileHelper。

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif/// .properties配置文件解析帮助类.
 3ExpandedBlockEnd.gif/// </summary>

 4 None.gif public   sealed   class  PropertiesFileHelper
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 7InBlock.gif    /// <value>缺省的注释符号.</value>
 8ExpandedSubBlockEnd.gif    /// </summary>

 9InBlock.gif    public static readonly string DEAFULT_NOTE_DENOTATION = @"#";
10InBlock.gif
11ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
12InBlock.gif    /// <value>缺省的key/value分隔符.</value>
13ExpandedSubBlockEnd.gif    /// </summary>

14InBlock.gif    public static readonly string DEFAULT_DELIMITER = @"=";
15InBlock.gif
16InBlock.gif    private PropertiesFileHelper()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18ExpandedSubBlockEnd.gif    }

19InBlock.gif
20ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
21InBlock.gif    /// 解释一个.properties的配置文件.
22InBlock.gif    /// </summary>
23InBlock.gif    /// <param name="filePath">文件路径.</param>
24ExpandedSubBlockEnd.gif    /// <returns>key/value pair table.</returns>

25InBlock.gif    public static Hashtable ParsePropertiesFile(string filePath)
26ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
27InBlock.gif        if (!File.Exists(filePath))
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
29InBlock.gif            throw new FileNotFoundException(filePath);
30ExpandedSubBlockEnd.gif        }

31InBlock.gif
32InBlock.gif        Hashtable _keyValues = new Hashtable();
33InBlock.gif
34InBlock.gif        string textLine;
35InBlock.gif        using (StreamReader sr = new StreamReader(filePath))
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37InBlock.gif            while (sr.Peek() >= 0)
38ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
39InBlock.gif                textLine = sr.ReadLine();
40InBlock.gif                if (textLine.Trim().Length > 0)
41ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
42InBlock.gif                    ParsePropertiesLine(_keyValues, textLine);
43ExpandedSubBlockEnd.gif                }

44ExpandedSubBlockEnd.gif            }

45ExpandedSubBlockEnd.gif        }

46InBlock.gif
47InBlock.gif        return _keyValues;
48ExpandedSubBlockEnd.gif    }

49InBlock.gif
50InBlock.gif    private static void ParsePropertiesLine(Hashtable keyValues, string textLine)
51ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
52InBlock.gif        if (string.IsNullOrEmpty(textLine))
53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
54InBlock.gif            return;
55ExpandedSubBlockEnd.gif        }

56InBlock.gif
57InBlock.gif        // 注释.
58InBlock.gif        if (textLine.StartsWith(DEAFULT_NOTE_DENOTATION))
59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
60InBlock.gif            return;
61ExpandedSubBlockEnd.gif        }

62InBlock.gif            
63InBlock.gif        int pos = textLine.IndexOf(DEFAULT_DELIMITER);
64InBlock.gif        if (pos > 0)
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            keyValues.Add(textLine.Substring(0, pos).Trim(), textLine.Substring(pos + DEFAULT_DELIMITER.Length, textLine.Length - pos -DEFAULT_DELIMITER.Length));
67ExpandedSubBlockEnd.gif        }

68ExpandedSubBlockEnd.gif    }

69ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/bengxia/archive/2006/05/09/395551.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值