I have had a good look around here but haven't quite found what I am looking for. I am reading an ini file, using my ini class, I can call each section and all keys and values but I have an issue with section [Races]. where it says IOM=7 and UK=6, I want to make a call to the key IOM and for loop through the races 1-6 and do the same for any other key value pair in that section. Here is my code.
List<string> PlacesList= new List<string>();
List<string> PositionsList= new List<string>();
public void btnReadini_Click(object sender, EventArgs e)
{
PlacesList = ListEntries("Places");
PositionsList = ListEntries("Positions");
}
public List<string> ListEntries(string sectionName)
{
IniFile INI = new IniFile(@"C:\Races.ini");
List<string> entries = null;
string[] Entry = INI.GetEntryKeyNames(sectionName);
if (Entry != null)
{
entries = new List<string>();
foreach (string EntName in Entry)
{
entries.Add(EntName + "=" + INI.GetEntryKeyValue(sectionName, EntName));
}
}
return entries;
}
here is my class:
public class IniFile
{
[DllImport("kernel32")]
static extern int GetPrivateProfileString(string Section, string Key,
string Value, StringBuilder Result, int Size, string FileName);
[DllImport("kernel32")]
static extern int GetPrivateProfileString(string Section, int Key,
string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result,
int Size, string FileName);
[DllImport("kernel32")]
static extern int GetPrivateProfileString(int Section, string Key,
string Value, [MarshalAs(UnmanagedType.LPArray)] byte[] Result,
int Size, string FileName);
public string path;
public IniFile(string INIPath)
{
path = INIPath;
}
public string[] GetSectionNames()
{
for (int maxsize = 500; true; maxsize *= 2)
{
byte[] bytes = new byte[maxsize];
int size = GetPrivateProfileString(0, "", "", bytes, maxsize, path);
if (size < maxsize - 2)
{
string Selected = Encoding.ASCII.GetString(bytes, 0,
size - (size > 0 ? 1 : 0));
return Selected.Split(new char[] { '\0' });
}
}
}
public string[] GetEntryKeyNames(string section)
{
for (int maxsize = 500; true; maxsize *= 2)
{
byte[] bytes = new byte[maxsize];
int size = GetPrivateProfileString(section, 0, "", bytes, maxsize, path);
if (size < maxsize - 2)
{
string entries = Encoding.ASCII.GetString(bytes, 0,
size - (size > 0 ? 1 : 0));
return entries.Split(new char[] { '\0' });
}
}
}
public object GetEntryKeyValue(string section, string entry)
{
for (int maxsize = 250; true; maxsize *= 2)
{
StringBuilder result = new StringBuilder(maxsize);
int size = GetPrivateProfileString(section, entry, "",
result, maxsize, path);
if (size < maxsize - 1)
{
return result.ToString();
}
}
}
}
And here is my ini file:
[Places]
IOM=Isle of man
UK=United Kingdom
IRE=Ireland
[Races]
IOM=7
UK=6
[Positions]
WN=Win
2nd=Second
3rd=Third
4th=Fourth
I know I will need to for loop through races 1-7 like this:
For(int i = 1; i < 7) i++)
I'm just not sure how to call to the ini and do that part. What i would like to do in the end is something like this:
foreach (IOM Isle of man)
{
for( 1 - 7)
{
foreach(Win - Fourth)
{
listBox1.Items.Add(the result of the above);
}
}
}