public static IniData ReadINI(string content) { #pragma warning disable CS0618 var parser = new StringIniParser(); #pragma warning restore CS0618 return(parser.ParseString(content)); }
public IniFileParser(string filepath) { if (filepath == null) { throw new NullReferenceException(); } if (!new Regex(@".*\.ini").IsMatch(filepath)) { throw new WrongFileFormatException(); } string fileText; try { // reading file and deleting comments fileText = Regex.Replace(File.ReadAllText(filepath), @";.*$", "", RegexOptions.Multiline); } catch (Exception) { throw new FileReadingException(); } try { _data = new StringIniParser().ParseString(fileText); } catch (Exception) { throw new FileReadingException(); } }
public void check_that_comment_char_is_not_stored_as_the_key() { string data = "[data]" + Environment.NewLine + "key = value;"; IniData inidata = new StringIniParser().ParseString(data); Assert.That(inidata["data"]["key"], Is.EqualTo("value;")); }
private static (string, string) ParsePropertyFile(string filepath) { IniData data = new StringIniParser().ParseString(File.ReadAllText(filepath)); string driver = data["CATALOG_PROPERTIES"]["driver"]; string connectionString = data["CATALOG_PROPERTIES"]["connection-string"]; return(driver, connectionString); }
public void allow_keys_with_dots() { string strTest = "[section_issue.3]\nkey.with_dots = value\n"; IniData data = new StringIniParser().ParseString(strTest); Assert.That(data.Sections.Count, Is.EqualTo(1)); Assert.That(data.Sections["section_issue.3"]["key.with_dots"], Is.Not.Null); }
public void test_no_exception_is_raised_when_reading_url_like_section_names() { string data = @"[http://example.com/page] key1 = value1"; IniData newData = new StringIniParser().ParseString(data); Assert.That(newData.Sections[@"http://example.com/page"]["key1"], Is.EqualTo("value1")); }
public void not_reproduced_error_tests() { string test = "[ExampleSection]\nkey = value;value\n"; StringIniParser strParser = new StringIniParser(); IniData data = strParser.ParseString(test); Assert.That(data.Sections.Count, Is.EqualTo(1)); Assert.That(data.Sections["ExampleSection"], Is.Not.Null); Assert.That(data.Sections["ExampleSection"].Count, Is.EqualTo(1)); Assert.That(data.Sections["ExampleSection"]["key"], Is.EqualTo("value")); }
public void WritingTotring_Test() { StringIniParser parser = new StringIniParser(); IniData data = new IniData(); data.Sections.AddSection("newSection1"); data.Sections["newSection1"].AddKey("newKey1", "newValue1"); data.Sections["newSection1"].AddKey("newKey2", "newValue5"); string result = parser.WriteString(data); Assert.That(result, Is.Not.Empty); Assert.That(result.Length, Is.Not.EqualTo(0)); }
public void check_using_another_leading_character_for_comments() { string data = @"[test] # a comment connectionString = Server=sqlserver.domain.com;Database=main;User ID=user;Password=password"; StringIniParser parser = new StringIniParser(); parser.Parser.Configuration.CommentString = "#"; IniData iniData = parser.ParseString(data); Assert.That( iniData["test"]["connectionString"], Is.EqualTo("Server=sqlserver.domain.com;Database=main;User ID=user;Password=password")); Assert.That( iniData["test"].GetKeyData("connectionString").Comments[0], Is.EqualTo(" a comment")); }
public void WriteToFile(string filePath, string iniSection, IDictionary <string, string> data) { try { KeyDataCollection keyDataCollection = DictionaryToKeyDataCollection(data); IniData iniData = new IniData(); SectionData sectionData = new SectionData(iniSection); sectionData.Keys = keyDataCollection; iniData.Sections.SetSectionData(iniSection, sectionData); StringIniParser iniParser = new StringIniParser(); FileUtils.RemoveReadOnly(filePath); string fileContents = iniParser.WriteString(iniData); File.WriteAllText(filePath, fileContents); } catch (Exception e) { this.LogError("WriteToFile() error: " + e.ToString()); } }
private IDictionary <string, string> Parse(string data, string iniSection) { if (String.IsNullOrEmpty(data)) { this.LogError("Parse() error: empty or null content"); return(null); } IniData iniData = null; try { StringIniParser iniParser = new StringIniParser(); iniData = iniParser.ParseString(data); } catch (Exception e) { this.LogError("Parse() error: " + e.ToString()); } if (iniData == null) { this.LogError("Parse() error: ini parse failed due to invalid data"); return(null); } KeyDataCollection keyDataCollection = null; try { keyDataCollection = iniData[iniSection]; } catch (Exception e) { this.LogError("Parse() exception: " + e.ToString()); return(null); } if (keyDataCollection == null) { this.LogError("Parse() error: could not find section '" + iniSection + "' in ini"); return(null); } return(KeyDataCollectionToDictionary(keyDataCollection)); }