2

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);
}
}
}
4
  • 2
    I would really recommend using an XML file instead, i/o and iteration are much simpler with something like Linq-to-Xml. The PrivateProfile APIs are pretty horrible under the hood.
    – Alex K.
    Aug 16, 2017 at 16:07
  • I don't fully understand the question. I can't see those 6 races in your ini file. What do you want to iterate through?
    – René Vogt
    Aug 16, 2017 at 16:07
  • Hi Rene, when I read the file it says 6 or 7 races, instead of writing in the ini file, IOM 1, IOM 2, IOM 3 etc it just says IOM 7. That means there are seven races happening in IOM but if I just read the value and key in normally I will only see IOM 7. So I need to in some way have my code understand that if the ini says IOM 7, it needs to check races 1 - 7. I Hope that makes a bit more sense. I am not using XML because the ini file is easier to manually update with information.
    – Oxx
    Aug 17, 2017 at 8:30
  • With the explanation you just provided, I'm joining @AlexK. in saying that you should look into using XML. Ini files are great for initialising individual parameters. You're trying to put a complex data structure into a flat list of parameters. While technically not impossible, it is needlessly complicated. Using XML you can stop yourself from having to think of clever tricks to work around the INI file limitations.
    – Flater
    Sep 18, 2017 at 8:38

2 Answers 2

1

here is how could iterate through all the sections and all the sections's keys:

IniFile ini = new IniFile();
ini.Load("path to your .ini file ...");

foreach (IniSection section in ini.Sections)
{
    foreach (IniKey key in section.Keys)
    {
        string sectionName = section.Name;
        string keyName = key.Name;
        string keyValue = key.Value;

        // Do something ...
    }
}
1

I sorted this by creating a class of places, in the places class I made a method called noOfRaces. When I use the function in my code it looks like this:

List<Place> placeList = new List<Place>();
public class Place
{
private string _Program;
    private string _Name;
    public int _NoOfRaces
    { get; set; }

    public string Program
    { get { return _Program; } }

    public string Name
    { get { return _Name; } }

    public Place(string pKey, string pValue)
    {
        _Program = pKey;
        _Name = pValue;
    }

    public void setNoOfRaces(int pRaces)
    {
        _NoOfRaces = pRaces;
    }
}

Then in my .cs I use the above classes like so:

        private int findNoOfRaces(Place pPlace)
    {
        foreach (Place program in placeList)
        {
            foreach (string line in raceDetails)
            {
                string raceNoTotal = "";
                if (line.StartsWith(pTrack.Program + "="))
                {
                    char delimiter = '=';
                    string value = line;
                    string[] raceSubstring = value.Split(delimiter);
                    raceNoTotal = raceSubstring[1];

                    for (int i = 1; i <= Convert.ToInt32(raceNoTotal); i++)
                    {
                        foreach (Pool nextPool in poolList)
                        {
                            listBox1.Items.Add(pTrack.Program + " " + " " + nextPool.poolName + " " +i);
                        }
                    }
                    return Convert.ToInt32(raceNoTotal);
                }
            }
        }
        return -1;
    }

This now gives me the output i wanted which looks like this:

IOM Win 1
IOM second 1
IOM Third 1
IOM Fourth 1
IOM Win 2
IOM second 2
IOM Third 2
IOM Fourth 2

All the way to 7 for IOM and UK goes all the way to 6. By using the function ican for loop through all the races in the ini.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.