ini文件的读取

写了个读取
形如

None.gif [ Database Info ]
None.gifGroupNum 
=   3
None.gifCharSet 
=   3383
None.gif
None.gif
[ drivers ]
None.gifwave
= mmdrv.dll
None.giftimer
= timer.drv
ini配置文件读取的方法如下:
None.gif Hashtable ReadingINI( string  fileName)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    Hashtable htSection 
= new Hashtable();
InBlock.gif    Hashtable htKey 
= new Hashtable();
InBlock.gif    System.Text.Encoding encode 
= System.Text.Encoding.GetEncoding("GB2312");
InBlock.gif
InBlock.gif    
string fileDataLine = null;
InBlock.gif    Match match;
InBlock.gif    Regex regSection 
= new Regex(@"^\[(?<key>.+)\]$");
InBlock.gif    Regex keySection 
= new Regex(@"^(?<key>.+)=(?<value>.+)$");
InBlock.gif
InBlock.gif    StreamReader streamReader 
= new StreamReader(fileName, encode);
InBlock.gif    fileDataLine 
= streamReader.ReadLine();
InBlock.gif
InBlock.gif    
while (fileDataLine != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
if (!(fileDataLine.StartsWith("#"|| fileDataLine.StartsWith(";") ))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            match 
= keySection.Match(fileDataLine);
InBlock.gif            
if (match.Success)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                htKey.Add(match.Result(
"${key}"), match.Result("${value}"));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                match 
= regSection.Match(fileDataLine);
InBlock.gif                
if (match.Success)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    htKey 
= new Hashtable();
InBlock.gif                    htSection.Add(match.Result(
"${key}"), htKey);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        fileDataLine 
= streamReader.ReadLine();
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    streamReader.Close();
InBlock.gif    streamReader 
= null;
InBlock.gif
InBlock.gif    
return htSection;
ExpandedBlockEnd.gif}

假设用上面的代码读上上面的配置文件, 返回值名字是 htINI
则可用如下方法取出timer对应的值
None.gif Hashtable htSection  =  htINI[ " drivers " as  Hashtable;
None.gif
if null   !=  htSection)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return htSection["timer"];
ExpandedBlockEnd.gif}

None.gif
else
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return string.Empty;
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/sun_moon_earth/archive/2006/12/14/592339.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值