/// <summary> /// Creates a new IniFile that is loaded from a path. /// </summary> /// <param name="filePath">Loads from Path</param> public IniFile(string filePath) : this() { using (StreamReader rdr = new StreamReader(filePath)) { IniParameters currentSection = Parameters; String line; int lineNum = 0; while ((line = rdr.ReadLine()) != null) { lineNum++; //if it is a setting it is added to the current section //if it is a section the current section is changed and added to the file //otherwise, if the line is not blank or commented an error is thrown Match settingMatch = settingPattern.Match(line); if (settingMatch.Success) { //keys are stored as uppercase string key = settingMatch.Groups["Key"].Value.Trim(); string value = settingMatch.Groups["Value"].Value.Trim(); //Uncommand and save the following if you wish to record comments // string comment = settingMatch.Groups[""].Value.Trim(); currentSection.Add(key, Unescape(value)); continue; } Match sectionMatch = sectionPattern.Match(line); if (sectionMatch.Success) { IniSection section = new IniSection(sectionMatch.Groups["Name"].Value); currentSection = section.Parameters; Sections.Add(section); continue; } if (!emptyLinePattern.IsMatch(line)) { throw new InvalidDataException("Input file contains incorrect formatting on line " + lineNum.ToString()); } } } FilePath = filePath; }
//a pattern gifted unto thee by the regex gods // private const string INI_SETTING_PATTERN = @" // ^\s* #accounts for leading spaces // (?<Key> #start of key group // [A-Za-z0-9_-]+ #key is made up of letters, digits and _ or - // ) // \s* #accounts for spaces before delimiter // [=:] #delimiter may be a : or = // \s* #spaces after delimiter // (?<Value> #value group // [^\s#;:=]+? #at least one non-whitespace or comment character, lazy // ((?<=\\)[=:]|[^=:])*? #doesnt match unescaped = or :, is lazy to exclude whitespace and comments // ) // \s* #account for spaces before comment or after value // ((?<!\\)[#;].*)? #a comment starting with # or ; // $ #end of string"; /// <summary> /// Gets or sets the setting with name key in the specified section. /// If it does not exist setting it will create it and getting it will return null. /// </summary> /// <param name="sectionName">name of section to place key in.</param> /// <param name="key">the unique key name linked to this setting</param> /// <returns>value associated with key or null if it does not exist</returns> public string this[string sectionName, string key] { get { if (Sections.ContainsKey(sectionName) && Sections[sectionName].Parameters.ContainsKey(key)) { return(Sections[sectionName].Parameters[key]); } else { return(null); } } set { if (Sections.ContainsKey(sectionName)) { IniParameters p = Sections[sectionName].Parameters; if (p.ContainsKey(key)) { p[key] = value; } else { p.Add(key, value); } } else { IniSection section = new IniSection(sectionName); section.Parameters.Add(key, value); Sections.Add(section); } } }
public IniSection(string sectionName) { SectionName = sectionName; Parameters = new IniParameters(); }
/// <summary> /// Creates a new, empty, INI file. /// </summary> public IniFile() { Sections = new IniSections(); Parameters = new IniParameters(); FilePath = ""; }