pinoyprogammer
Techie
IniFileParser cs file
Code:
using System;
namespace PCO
{
public class IniFileParser
{
public string location;
public IniFileParser(string location)
{
this.location = location;
}
public string getSettingValue(string settingName)
{
try
{
string returnValue = "";
string entireText = System.IO.File.ReadAllText(location);
int settingPosition = entireText.IndexOf(settingName);
bool readStart = false;
if (settingPosition != -1)
{
for (int i = settingPosition; i < entireText.Length; i++)
{
if (readStart)
{
if (entireText[i] != ';')
{
returnValue += entireText[i];
}
else
{
return returnValue;
}
}
else
{
if (entireText[i] == '=')
{
readStart = true;
}
}
}
}
return returnValue;
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.StackTrace);
System.Windows.Forms.MessageBox.Show("Cannot find file settings.ini\nStack trace: " + ex.StackTrace);
return null;
}
}
public void setSettingValue(string settingName, string settingValue)
{
try
{
string entireText = System.IO.File.ReadAllText(location);
int settingStartPos = entireText.IndexOf(settingName);
string temp = "";
for (int x = settingStartPos; x < entireText.Length; x++)
{
if (entireText[x] == ';')
{
break;
}
else
{
temp += entireText[x];
}
}
entireText = entireText.Replace(temp, settingName + " = " + settingValue + "");
System.IO.File.WriteAllText(location, entireText);
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.StackTrace);
System.Windows.Forms.MessageBox.Show("Cannot find file settings.ini\nStack trace: "+ex.StackTrace);
}
}
}
}