public void SaveToStream() { string filePath = "SaveToStream.ini"; FileStream stream = new FileStream(filePath, FileMode.Create); // Create a new document and save to stream IniDocument doc = new IniDocument(); IniSection section = new IniSection("Pets"); section.Set("dog", "rover"); section.Set("cat", "muffy"); doc.Sections.Add(section); doc.Save(stream); stream.Close(); IniDocument newDoc = new IniDocument(new FileStream(filePath, FileMode.Open)); section = newDoc.Sections["Pets"]; Assert.IsNotNull(section); Assert.AreEqual(2, section.GetKeys().Length); Assert.AreEqual("rover", section.GetValue("dog")); Assert.AreEqual("muffy", section.GetValue("cat")); stream.Close(); File.Delete(filePath); }
public void WindowsStyleDocument() { StringWriter writer = new StringWriter(); writer.WriteLine("; another comment"); // empty line writer.WriteLine("[test]"); writer.WriteLine(" cat = cats are not ; tall "); writer.WriteLine(" dog = dogs \"bark\""); IniDocument doc = new IniDocument(new StringReader(writer.ToString()), IniFileType.WindowsStyle); IniSection section = doc.Sections["test"]; Assert.AreEqual("cats are not ; tall", section.GetValue("cat")); Assert.AreEqual("dogs \"bark\"", section.GetValue("dog")); }
public void SetKey() { IniDocument doc = new IniDocument(); IniSection section = new IniSection("new section"); doc.Sections.Add(section); section.Set("new key", "some value"); Assert.IsTrue(section.Contains("new key")); Assert.AreEqual("some value", section.GetValue("new key")); }
/// <summary> /// Loads the file not saving comments. /// </summary> private void LoadReader(IniReader reader) { reader.IgnoreComments = false; var sectionFound = false; IniSection section = null; try { while (reader.Read()) { switch (reader.Type) { case IniType.Empty: if (!sectionFound) { initialComment.Add(reader.Comment); } else { section.Set(reader.Comment); } break; case IniType.Section: sectionFound = true; // If section already exists then overwrite it if (sections[reader.Name] != null) { sections.Remove(reader.Name); } section = new IniSection(reader.Name, reader.Comment); sections.Add(section); break; case IniType.Key: if (section.GetValue(reader.Name) == null) { section.Set(reader.Name, reader.Value, reader.Comment); } break; } } } catch (Exception ex) { throw; } finally { // Always close the file reader.Close(); } }
public void GetKey() { StringWriter writer = new StringWriter(); writer.WriteLine("[Nini]"); writer.WriteLine(" my key = something"); IniDocument doc = new IniDocument(new StringReader(writer.ToString())); IniSection section = doc.Sections["Nini"]; Assert.IsTrue(section.Contains("my key")); Assert.AreEqual("something", section.GetValue("my key")); Assert.IsFalse(section.Contains("not here")); }
/// <summary> /// Loads the file not saving comments. /// </summary> private void LoadReader(IniReader reader) { reader.IgnoreComments = false; var sectionFound = false; IniSection section = null; try { while(reader.Read()) { switch(reader.Type) { case IniType.Empty: if(!sectionFound) { initialComment.Add(reader.Comment); } else { section.Set(reader.Comment); } break; case IniType.Section: sectionFound = true; // If section already exists then overwrite it if(sections[reader.Name] != null) { sections.Remove(reader.Name); } section = new IniSection(reader.Name, reader.Comment); sections.Add(section); break; case IniType.Key: if(section.GetValue(reader.Name) == null) { section.Set(reader.Name, reader.Value, reader.Comment); } break; } } } catch(Exception ex) { throw; } finally { // Always close the file reader.Close(); } }