如何读取 ini 文件部分的值和详细信息,然后循环遍历它 c#

How to read an ini file section value and detail then for loop through it c#

我仔细看了看这里,但还没有完全找到我要找的东西。我正在读取一个 ini 文件,使用我的 ini class,我可以调用每个部分以及所有键和值,但我对部分 [Races] 有疑问。它显示 IOM=7 和 UK=6,我想调用键 IOM 并循环遍历 1-6 的比赛,并对该部分中的任何其他键值对执行相同的操作。这是我的代码。

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;
}

这是我的 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[] { '[=11=]' });
        }
    }
}

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[] { '[=11=]' });
        }
    }
}

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();
        }
    }
}

}

这是我的 ini 文件:

[Places]
IOM=Isle of man
UK=United Kingdom
IRE=Ireland
[Races]
IOM=7
UK=6
[Positions]
WN=Win
2nd=Second
3rd=Third
4th=Fourth

我知道我需要像这样在第 1-7 场比赛中循环:

For(int i = 1; i < 7) i++)

我只是不确定如何调用 ini 并执行该部分。 最后我想做的是这样的:

foreach (IOM Isle of man)
{
for( 1 - 7)
{
foreach(Win - Fourth)
{
 listBox1.Items.Add(the result of the above);
}
}
}

这里是如何遍历所有部分和所有部分的键:

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 ...
    }
}

我通过创建一个 class 个地方来对此进行排序,在这些地方 class 我创建了一个名为 noOfRaces 的方法。当我在我的代码中使用该函数时,它看起来像这样:

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;
    }
}

然后在我的 .cs 中我使用上面的 classes 像这样:

        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;
    }

这现在给了我想要的输出,如下所示:

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

国际移民组织一直到 7 英国一直到6。通过使用函数ican for循环遍历ini中的所有比赛。