ini文件的读取
写了个读取
形如
[Database Info]
GroupNum = 3
CharSet = 3383
[drivers]
wave=mmdrv.dll
timer=timer.drv
ini配置文件读取的方法如下:GroupNum = 3
CharSet = 3383
[drivers]
wave=mmdrv.dll
timer=timer.drv
Hashtable ReadingINI(string fileName)
{
Hashtable htSection = new Hashtable();
Hashtable htKey = new Hashtable();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
string fileDataLine = null;
Match match;
Regex regSection = new Regex(@"^\[(?<key>.+)\]$");
Regex keySection = new Regex(@"^(?<key>.+)=(?<value>.+)$");
StreamReader streamReader = new StreamReader(fileName, encode);
fileDataLine = streamReader.ReadLine();
while (fileDataLine != null)
{
if (!(fileDataLine.StartsWith("#") || fileDataLine.StartsWith(";") ))
{
match = keySection.Match(fileDataLine);
if (match.Success)
{
htKey.Add(match.Result("${key}"), match.Result("${value}"));
}
else
{
match = regSection.Match(fileDataLine);
if (match.Success)
{
htKey = new Hashtable();
htSection.Add(match.Result("${key}"), htKey);
}
}
}
fileDataLine = streamReader.ReadLine();
}
streamReader.Close();
streamReader = null;
return htSection;
}
{
Hashtable htSection = new Hashtable();
Hashtable htKey = new Hashtable();
System.Text.Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
string fileDataLine = null;
Match match;
Regex regSection = new Regex(@"^\[(?<key>.+)\]$");
Regex keySection = new Regex(@"^(?<key>.+)=(?<value>.+)$");
StreamReader streamReader = new StreamReader(fileName, encode);
fileDataLine = streamReader.ReadLine();
while (fileDataLine != null)
{
if (!(fileDataLine.StartsWith("#") || fileDataLine.StartsWith(";") ))
{
match = keySection.Match(fileDataLine);
if (match.Success)
{
htKey.Add(match.Result("${key}"), match.Result("${value}"));
}
else
{
match = regSection.Match(fileDataLine);
if (match.Success)
{
htKey = new Hashtable();
htSection.Add(match.Result("${key}"), htKey);
}
}
}
fileDataLine = streamReader.ReadLine();
}
streamReader.Close();
streamReader = null;
return htSection;
}
假设用上面的代码读上上面的配置文件, 返回值名字是 htINI
则可用如下方法取出timer对应的值
Hashtable htSection = htINI["drivers"] as Hashtable;
if( null != htSection)
{
return htSection["timer"];
}
else
{
return string.Empty;
}
if( null != htSection)
{
return htSection["timer"];
}
else
{
return string.Empty;
}