我已經在這裏很好看,但還沒有完全找到我在找什麼。我正在閱讀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);
}
}
}
我真的會推薦使用XML文件來代替,I/O和迭代更像Linq-to-Xml一樣簡單。 PrivateProfile API在後臺非常糟糕。 –
我不完全理解這個問題。我無法在你的ini文件中看到這6場比賽。你想要迭代什麼? –
嗨,雷恩,當我讀文件說6或7的比賽,而不是寫ini文件,IOM 1,IOM 2,IOM 3等它只是說IOM 7.這意味着有7場比賽發生在國際移民組織,但如果我只是通常閱讀價值和關鍵,我只會看到IOM 7.所以我需要以某種方式讓我的代碼明白,如果ini說IOM 7,它需要檢查1 - 7的比賽。我希望這會讓我有點更有意義。我不使用XML,因爲ini文件更容易手動更新信息。 – Oxlade