[Release] InI File Reader

06/28/2009 01:29 TheLeGend209#1
Well This Should be All.

Quote:
public class IniFile2
{
public string FileName;

public IniFile2()
{
}

public IniFile2(string _FileName)
{
this.FileName = _FileName;
}

[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int GetPrivateProfileStringA(string Section, string Key, string _Default, StringBuilder Buffer, int BufferSize, string FileName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int WritePrivateProfileStringA(string Section, string Key, string Arg, string FileName);

public byte ReadByte(string Section, string Key, byte _Default)
{
byte buf = _Default;
byte.TryParse(this.ReadString(Section, Key, _Default.ToString(), 6), out buf);
return buf;
}

public short ReadInt16(string Section, string Key, short _Default)
{
short buf = _Default;
short.TryParse(this.ReadString(Section, Key, _Default.ToString(), 9), out buf);
return buf;
}

public int ReadInt32(string Section, string Key, int _Default)
{
int buf = _Default;
int.TryParse(this.ReadString(Section, Key, _Default.ToString(), 15), out buf);
return buf;
}

public sbyte ReadSByte(string Section, string Key, byte _Default)
{
sbyte buf = (sbyte)_Default;
sbyte.TryParse(this.ReadString(Section, Key, _Default.ToString(), 6), out buf);
return buf;
}

public string ReadString(string Section, string Key, string _Default)
{
return this.ReadString(Section, Key, _Default, 255);
}

public string ReadString(string Section, string Key, string _Default, int BufSize)
{
StringBuilder Buffer = new StringBuilder(BufSize);
GetPrivateProfileStringA(Section, Key, _Default, Buffer, BufSize, this.FileName);
return Buffer.ToString();
}

public ushort ReadUInt16(string Section, string Key, ushort _Default)
{
ushort buf = _Default;
ushort.TryParse(this.ReadString(Section, Key, _Default.ToString(), 9), out buf);
return buf;
}

public uint ReadUInt32(string Section, string Key, uint _Default)
{
uint buf = _Default;
uint.TryParse(this.ReadString(Section, Key, _Default.ToString(), 15), out buf);
return buf;
}

public void Write(string Section, string Key, object Value)
{
WritePrivateProfileStringA(Section, Key, Value.ToString(), this.FileName);
}

public void Write(string Section, string Key, string Value)
{
WritePrivateProfileStringA(Section, Key, Value, this.FileName);
}

}
06/28/2009 01:59 Pete1990#2
You better give him the credits u know u didnt do this
06/28/2009 03:07 InfamousNoone#3
Weeeeeeeeeeeeeeeee I see a major problem with this class :D, Good job on conserving memory when reading specific data types though. Have fun figuring out the problem.