如何读取ini文件节的值和细节然后循环通过它c#

时间:2017-08-16 16:03:38

标签: c# ini

我在这里好好看看,但还没找到我想要的东西。我正在阅读一个ini文件,使用我的ini课程,我可以调用每个部分以及所有键和值,但我对[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;
}

这是我的班级:

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

}

这是我的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);
}
}
}

2 个答案:

答案 0 :(得分:0)

这里是如何迭代所有部分和所有部分的键:

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 :(得分:0)

我通过创建一个地方类来对此进行排序,在地方类中我创建了一个名为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中,我使用上面的类:

        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

IOM一直到7 通过使用函数ican循环遍历ini中的所有种族。