ind ellers vil den indlæse hele file hver gang du henter en sektion :)
Og selvfølgelig kan den også udvides med en collection af sektions navne, det er bare at læse ud fra iniSections collection, og returnere alle SecionName variabler :)
foreach (string section in sections) { IniSection iniSection = new IniSection(); iniSection.Values = new Dictionary<string, string>(); string[] elements = section.Split(new char[] {'\n'}, StringSplitOptions.RemoveEmptyEntries); for (int x = 0; x < elements.Length; x++) { if (x == 0) { //Første element er section name iniSection.SectionName = elements[x].Replace("]", "").Trim(); continue; } string[] parts = elements[x].Split(new char[] {'='}, StringSplitOptions.RemoveEmptyEntries); if (parts.Length == 2) { iniSection.Values.Add(parts[0].Trim(), parts[1].Trim()); }
} iniSections.Add(iniSection); }
parsed = true;
}
/// <summary> /// Gets the section contents. /// </summary> /// <param name="sectionName">Name of the section.</param> /// <param name="variableName">Name of the variable.</param> /// <returns></returns> public string GetSectionContents(string sectionName, string variableName) { if (!parsed) { Parse(); } IniSection iniSection = iniSections.Find(delegate(IniSection section) { return section.SectionName == sectionName; }); //Eller hvis .net 3.5 lambda section=>section.SectionName==sectionName if (iniSection == null) { return null; }
//sektion ikke fundet if (!iniSection.Values.ContainsKey(variableName)) { //variable ikke fundet return null; }
return iniSection.Values[variableName]; } }
public class IniSection { public string SectionName { get; set; }
public Dictionary<string, string> Values { get; set; } }
Som giver det følgende output: Total Kills:55 Total Deaths:65 Section blabla contains the following keys and values kills = 13 deaths = 24 Section ost contains the following keys and values kills = 42 deaths = 41 42
Error 1 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\User\Desktop\Program.cs 180 30 Error 2 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\User\Desktop\Program.cs 186 20 Error 3 The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\User\Desktop\Program.cs 202 20 Error 4 The type or namespace name 'Dictionary' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\User\Desktop\Program.cs 291 20
-> private readonly List<IniSection> iniSections = new List<IniSection>() -> public List<IniSection> AllSections -> public List<string> AllSectionNames -> public Dictionary<string, string> Values { get; set; }
Jeg tror jeg må finde en anden løsning, for jeg kan slet ikke få det til at virke. Ellers tak for hjælpen.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.