netmonitor/NetMonitorClient/InIReader.cs

75 lines
2.3 KiB
C#
Raw Normal View History

2010-04-19 13:18:26 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;
2010-04-21 13:48:40 +02:00
namespace NetMonitorClient
2010-04-19 13:18:26 +02:00
{
class InIReader
{
private StreamReader file;
private ArrayList cont = new ArrayList();
2010-04-19 13:18:26 +02:00
public InIReader(string filename)
{
this.file = new StreamReader(filename);
FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini");
2010-04-19 13:18:26 +02:00
k.Changed += new FileSystemEventHandler(this.readAgain);
loadFile();
}
private void readAgain(object sender, EventArgs e)
{
this.file.BaseStream.Seek(0, SeekOrigin.Begin);
loadFile();
}
private void loadFile()
{
string fline = "";
while (fline != null)
{
fline = this.file.ReadLine();
if (fline != null)
cont.Add(fline);
}
}
public ArrayList getSections()
{
ArrayList ret = new ArrayList();
foreach (string sOutput in this.cont)
{
Match match = Regex.Match(sOutput, @"^\[[a-zA-Z0-9\-_]+\]\w*$", RegexOptions.IgnoreCase);
2010-04-19 13:18:26 +02:00
if (match.Success)
ret.Add(sOutput);
}
return ret;
}
public String getValue(String section, String key)
{
bool sec = false;
foreach (string sOutput in this.cont)
{
if (sOutput == section)
sec = true;
if (sec && Regex.Match(sOutput, @"^\w*$").Success)
sec = false;
Match match = Regex.Match(sOutput, @"^" + Regex.Escape(key) + "=", RegexOptions.IgnoreCase);
if (match.Success && sec)
{
try
{
return sOutput.Split('=')[1];
}
catch (Exception)
{
return null;
}
}
}
return null;
}
2010-04-19 13:18:26 +02:00
}
}