ini文件的读取

写了个读取
形如

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

ini文件的读取            
else
ini文件的读取            
{
ini文件的读取                match 
= regSection.Match(fileDataLine);
ini文件的读取                
if (match.Success)
ini文件的读取                
{
ini文件的读取                    htKey 
= new Hashtable();
ini文件的读取                    htSection.Add(match.Result(
"${key}"), htKey);
ini文件的读取                }

ini文件的读取            }

ini文件的读取        }

ini文件的读取
ini文件的读取        fileDataLine 
= streamReader.ReadLine();
ini文件的读取    }

ini文件的读取
ini文件的读取    streamReader.Close();
ini文件的读取    streamReader 
= null;
ini文件的读取
ini文件的读取    
return htSection;
ini文件的读取}

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

ini文件的读取
else
ini文件的读取
{
ini文件的读取    
return string.Empty;
ini文件的读取}

你可能感兴趣的:(ini)