2017-08-16 19 views
1

我已經在這裏很好看,但還沒有完全找到我在找什麼。我正在閱讀ini文件,使用我的ini類,我可以調用每個部分和所有的鍵和值,但是我對[Races]部分有問題。它表示IOM = 7和UK = 6,我想調用密鑰IOM並通過比賽1-6進行循環,並對該部分中的任何其他鍵值對執行相同的操作。這是我的代碼。如何讀取ini文件段的值和細節,然後通過它循環c#

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

我真的會推薦使用XML文件來代替,I/O和迭代更像Linq-to-Xml一樣簡單。 PrivateProfile API在後臺非常糟糕。 –

+0

我不完全理解這個問題。我無法在你的ini文件中看到這6場比賽。你想要迭代什麼? –

+0

嗨,雷恩,當我讀文件說6或7的比賽,而不是寫ini文件,IOM 1,IOM 2,IOM 3等它只是說IOM 7.這意味着有7場比賽發生在國際移民組織,但如果我只是通常閱讀價值和關鍵,我只會看到IOM 7.所以我需要以某種方式讓我的代碼明白,如果ini說IOM 7,它需要檢查1 - 7的比賽。我希望這會讓我有點更有意義。我不使用XML,因爲ini文件更容易手動更新信息。 – Oxlade

回答

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 

一路7 IOM 和英國去一路6.通過通過在ini所有的比賽用ICAN for循環的功能。

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 ... 
    } 
} 
相關問題